1

I'm trying to move a text field to the right when a button is pressed. It works fine, however when I click on another text field or any other element the text field re-appears in its current location. I browsed the internet and found it works when I don't have autolayout enabled, however I need autolayout for setting variable positions. Any advice? Here's the method that moves the box:

- (IBAction)EnterInfo:(id)sender {
    CGRect newFrame = _UsernameField.frame;
    newFrame.origin.x += 500;    // shift right by 500pts
    [UIView animateWithDuration:1.0
                     animations:^{
                         _UsernameField.frame = newFrame;
                     }];
}

New code for last question:

- (IBAction)EnterInfo:(id)sender {
    [UIView animateWithDuration:1.0
                     animations:^{
                         _usernameX.constant = 130;
                     }];
}

1 Answers1

2

If you have auto layout turned on, you can't manipulate the frame like that. You need a reference to a NSLayoutConstraint and update the constant.

like this:

myXConstraint.constant = originalX + 500;

Edit -- and then your animate block should look like this:

[UIView animateWithDuration:0.5 animations:^{
    [self.view layoutIfNeeded];
}];
Bek
  • 2,206
  • 20
  • 35
  • So how would I access the x constraint of the _UsernameField? I can't find the .constraint property on it and I have had very very little experience with constraints – Matt Goodrich Jan 12 '15 at 23:17
  • If you set up your constraints in your xib/storyboard, you can create an IBOutlet for the constraint the same way you create an IBOutlet for any other UI element. If you didn't explicitly set any constraints in your view, now's the time to do it and then create the outlet to your x position constraint. – Bek Jan 12 '15 at 23:18
  • That makes so much more sense now! Is the auto constraint method like this the best/standard way to do it with animations? And thanks for the answer, I'll set it as the solution when it allows me to. – Matt Goodrich Jan 12 '15 at 23:21
  • Yep, that's the way to manipulate views on screen since Auto Layout came out. The way you have it in your post is how we used to do it pre-iOS 6 (or iOS 7 for me because Auto Layout was pretty rough when it first came out). – Bek Jan 12 '15 at 23:23
  • That makes sense, I'll definitely set everything up how you have described. My last question is the constraint update now works properly and holds its new position, however the transition is no longer animated. Editted original post with new code – Matt Goodrich Jan 12 '15 at 23:26
  • Sorry, I forgot the most important part! See my edited answer. Set the constraint outside the animation block and then call `layoutIfNeeded` – Bek Jan 12 '15 at 23:29
  • Perfect! That works exactly how I imagined it originally, thanks for the help! Is there any reference for animations that I can look at if I want the animations to follow one path over another? For example have it follow a parabolic path instead of just a straight one? – Matt Goodrich Jan 12 '15 at 23:36
  • I usually just google things: http://stackoverflow.com/questions/10869509/how-to-animate-a-uibezierpath looks like you may have to use a different animation method. – Bek Jan 12 '15 at 23:41
  • Okay, I was just wondering for the future, but for now what you gave me is perfect. Thanks a ton! – Matt Goodrich Jan 13 '15 at 00:00