UIView automatically moving by uitextfield keyboard

北战南征 提交于 2019-12-12 03:25:16

问题


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;
}

回答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.




回答2:


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];
}];



回答3:


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.



来源:https://stackoverflow.com/questions/28110886/uiview-automatically-moving-by-uitextfield-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!