0

So i have a really weird problem at my hands and hours of search has provided nothing.

I have a uiview containing a uitextfield. Now initially this view is outside of the visible screen with coordinates like x=-500,y=-500.

However in response to a button press this view animates and moves into the center of the screen.

Now whenever i tap on a uitextfield that is the subview of this view.This view moves back to its original coordinates outside the screen.

I have frantically checked my code and there is nothing that is moving the view outside again once its in. Any help in explaining this very unfamiliar behaviour would be really appreciated.

This code moves the view onto the screen

- (IBAction)Register:(id)sender {
//(self.view.frame.size.width/2)-(self.SignUp_Screen.frame.size.width/2);
//self.login_Screen.hidden = YES;
self.blurView.hidden = NO;
//self.SignUp_Screen.layer.zPosition = 5;
NSLog(@"Register");
self.SignUp_Screen.hidden = NO;

[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.SignUp_Screen.frame = CGRectMake(35, 50,self.SignUp_Screen.frame.size.width , self.SignUp_Screen.frame.size.height);
} completion:^(BOOL finished) {

}];
}

and these are the delegate methods for the textfield

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"TextFieldEndEditing");
    [textField resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"textFieldShouldReturn");
    [textField resignFirstResponder];
    return YES;
}
Win Coder
  • 6,628
  • 11
  • 54
  • 81

3 Answers3

1

As Wezly hints at, if you are using autolayout, you don't modify the frame directly anymore. That's the old world. You want to have an Outlet / property for the constraint and animate it.

[UIView animateWithDuration:0.25
                 animations:^{
                   SignUp_Screen.centerXConstraint.constant = ...;
                   SignUp_Screen.centerYConstraint.constant = ...;
                   [SignUp_Screen layoutIfNeeded];
 }];

See here and here for more details.

Community
  • 1
  • 1
0

The way i solved this problem was by linking an IBOutlet to the constraint I wanted to change and then animating it's constant value.

.h

@interface ViewController : UIViewController {
    IBOutlet NSLayoutConstraint *constraintHandle;
}

.m

[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    constraintHandle.constant = self.view.center.x;
    [SignUp_Screen layoutIfNeeded];
} completion:^(BOOL finished) {

}];

Don't forget to link the IBOutlet to your constraint in your storyboard or xib.

Wez
  • 10,555
  • 5
  • 49
  • 63
  • 2
    It is not necessary to put the constraint outlet in the header file, it is better to put it in a class extension in the implementation file so it is private to the class. It is really best to only put those things that should be public in the interface (.h) file. – zaph Jan 23 '15 at 13:52
0

You should not modify frame if you are using auto layout. You should animate view by animating constraint's constant. For example:

NSLayoutConstraint *viewY; //constraint from superview top to view top

viewY.constant = 100;
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:0.3f animations:^{
    [self.view layoutIfNeeded];
}];
Marcin P
  • 11
  • 3