iOS 8 中的 UIView 在 UITextField 编辑开始时不向上移动

UIView in iOS 8 Not Moving up when a UITextFiled Editing Begins

我写了下面的逻辑,用于在文本文件开始时向上移动视图它的 editing.It 在 iOS 7 中工作正常,但在 iOS 8 中不起作用。

代码:

-(void)viewDidLoad
{
  _leagalName.delegate = self;
  _phoneNumber.delegate = self;
  _email.delegate = self;
  _contactName.delegate = self;
  _contactNumber.delegate = self;
  _password.delegate = self;
  _confirmPassword.delegate = self;
}

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

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
  if([textField isEqual:_leagalName])
  {
    movementDistance = 0;
    [self animateTextField:textField up:YES];
  }
  else if([textField isEqual:_phoneNumber])
  {
    movementDistance = 0;
    [self animateTextField:textField up:YES];
  }
  else if([textField isEqual:_email])
  {
    movementDistance = -40;
    [self animateTextField:textField up:YES];
  }
  else if([textField isEqual:_contactName])
  {
    movementDistance = -80;
    [self animateTextField:textField up:YES];
  }
  else if([textField isEqual:_contactNumber])
  {
    movementDistance = -110;
    [self animateTextField:textField up:YES];
  }
  else if([textField isEqual:_password])
  {
    movementDistance = -140;
    [self animateTextField:textField up:YES];
  }
  else if([textField isEqual:_confirmPassword])
  {
    movementDistance = -170;
    [self animateTextField:textField up:YES];
  }
}
- (void)textFieldDidEndEditing:(UITextField *)textField
  {
    [self animateTextField:textField up:NO];
  }
- (BOOL)textFieldShouldReturn:(UITextField *)textField
  {
    [textField resignFirstResponder];
     return YES;
  }

任何人都可以解释一下我如何在 iOS 8.

中向上移动视图

在iOS8中,不建议在自动布局情况下在动画期间更改帧。 大多数开发人员使用的常用方法是相应地更新动画块中的约束。

如果你还想尝试设置框架的方法,或许你可以禁用自动布局并尝试一下。

请使用以下方法更新您UI

- (void)keyboardDidShow:(NSNotification *)note
{
[UIView animateWithDuration:0.5f animations:^ {
    if([super isIpad]){

            self.view.frame = CGRectMake(0, -350, 768, 1024);

    }
    else{
            self.view.frame = CGRectMake(0, -275, 320, 568);

        }
}];
}
-(void)keyboardDidhide:(NSNotification *)note
{


[UIView animateWithDuration:0.5f animations:^ {
    if([super isIpad]){

            self.view.frame = CGRectMake(0, 0, 768, 1024);

    }
    else{

            self.view.frame = CGRectMake(0, 0, 320, 568);

    }
}];
}

请记住,我正在使用 [UIWindow sharedWindow] 绘制我的笔尖,您必须使用您的屏幕尺寸,其次请在您的 viewDidLoad 方法中添加以下代码。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

`[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidhide:) name:UIKeyboardDidHideNotification object:nil];

`