当它从背景返回时,如何保留Uiview控制器的状态



我创建了一个登录页面,该页面具有两个uitextfield和登录按钮。当您尝试登录键盘hide textfield和按钮时,我已经使用以下代码从Textfield代表方法上下移动控制器。

-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
    int movementDistance = -130; // tweak as needed
    float movementDuration = 0.3f; // tweak as needed
    int movement = (up ? movementDistance : -movementDistance);
    [UIView beginAnimations: @"animateTextField" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

它可以正常工作。但是,当我通过"主页"按钮关闭应用程序并再次重新启动时,它仍不保留视图控制器位置,键盘仍在显示,但视图控制器的位置更改为默认值。

在视图控制器类的ViewDidload中声明nsnotification ,当应用程序将变得有效时,它将调用您的欲望方法。


-(void)viewDidLoad
            {
              [[NSNotificationCenter defaultCenter]addObserver:self
                                                 selector:@selector(refreshViewOnActive:)
                                                     name:UIApplicationDidBecomeActiveNotification
                                                   object:nil];

        // write rest of your code
            }
 - (void)refreshViewOnActive:(NSNotification *)notification {
       if( [textField isFirstResponder]) // check whether keyboard is open or not / or editing is enabled for your textfeild 
          {
            [self animateTextField:textField up:true]; //call your desired method
          }
    }

最新更新