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

时间:2015-01-27 07:18:55

标签: ios objective-c iphone uiview ios8

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

2 个答案:

答案 0 :(得分:1)

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

如果您仍想尝试设置框架方法,可能您可以禁用自动布局并试一试。

答案 1 :(得分:0)

请使用以下方法更新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];

`