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



我编写了以下逻辑,用于在文本开始编辑时向上移动视图。它在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];

'

最新更新