当键盘显示不起作用时,向上移动包含UITextField和UIButton的UIView

Moh*_*bir 0 keyboard objective-c subview uitextfield ios

在主视图中,我在UITableView另一个视图下面,textInputView包含a UITextField和a UIButton.

向上移动UIView包含UITextField,UIButton当键盘显示不起作用时.

-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
    const int movementDistance = -224; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? movementDistance : -movementDistance);

    [UIView beginAnimations: @"animateTextField" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    textInputView.frame = CGRectOffset(textInputView.frame, 0, movement);
    [UIView commitAnimations];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{

    [self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField:textField up:NO];
}
Run Code Online (Sandbox Code Playgroud)

如果我用self.view它替换textInputView 它会向上移动整个视图但我不想要.我只想向上移动textInputView.

这是视图层次结构:

查看heirarchy

这是我的实际观点:

在此输入图像描述

顺便说一句,我使用的是XCode 6iOS 8SDK ...

San*_*eep 5

由于您正在使用textInputView的框架,因此在不同的时间它是不同的,因此您无法获得所需的效果,

这样做,

[self.view bringSubviewToFront: textInputView]
if(up){
  textInputView.frame = CGRectOffset(textInputView.bounds, 0 self.view.bounds.size.height - textInputView.bounds.size.height)
}else{
  textInputView.frame = CGRectOffset(textInputView.bounds, 0 self.view.bounds.size.height)
}
Run Code Online (Sandbox Code Playgroud)

现在,我觉得你想在键盘出现时在键盘上方显示输入视图,并在键盘消失时隐藏它.首选方法是观察UIKeyboardWillShowNotificationUIKeyboardWillHideNotification.它还提供动画持续时间和最终键盘框架.

- (void)viewDidLoad{
  [super viewDidLoad];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}


- (void)keyboardWillShow:(NSNotification*)note{
  NSDictionary *userInfo = note.userInfo;
  CGRect finalKeyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

  float inputViewFinalYPosition = self.view.bounds.size.height - finalKeyboardFrame.size.height;
  CGRect inputViewFrame = textInputView.bounds;
  inputViewFrame.origin.y = inputViewFinalYPosition;

  [UIView animateWithDuration:animationDuration animations:^{
    textInputView.frame = inputViewFrame;
  }];

}

- (void)keyboardWillHide:(NSNotification*)note{

  NSDictionary *userInfo = note.userInfo;
  NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

  CGRect inputViewFrame = textInputView.bounds;
  inputViewFrame.origin.y = self.view.bounds.size.height;

  [UIView animateWithDuration:animationDuration animations:^{
    textInputView.frame = inputViewFrame;
  }];

}
Run Code Online (Sandbox Code Playgroud)