简体   繁体   English

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

[英]Move up the UIView containing UITextField and UIButton when Keyboard shows up not working

In the main view, I've a UITableView beneath that I've another view, textInputView that contains a UITextField and a UIButton . 在主视图中,下面有一个UITableView ,另一个视图是textInputView,其中包含一个UITextField和一个UIButton

Move up the UIView containing UITextField and UIButton when Keyboard shows not working. 当键盘显示不起作用时,向上移动包含UITextFieldUIButtonUIView

-(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];
}

If I replace the textInputView with the self.view it moves up the entire view but that I don't want. 如果我用替换textInputView self.view它移动了整个视图但我不想。 I just want to move up the textInputView. 我只想将textInputView向上移动。

Here is the view hierarchy : 这是视图层次结构

查看层次结构

Here is my actual view: 这是我的实际看法:

在此处输入图片说明

By the way, I'm using XCode 6 with iOS 8 SDK... 顺便说一句,我将XCode 6iOS 8 SDK结合使用...

Since you are using the frame for the textInputView, it is different at different times and so you are not getting the desired effect, 由于您使用的是textInputView的框架,因此在不同的时间会有所不同,因此无法获得理想的效果,

Do it like so, 像这样

[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)
}

Now, I feel that you want to show the input view just above the keyboard when keyboard appears and hide it when keyboard disappears. 现在,我觉得您想在出现键盘时在键盘上方显示输入视图,而在键盘消失时将其隐藏。 The preferred way to do is by observing for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification . 首选方法是观察UIKeyboardWillShowNotificationUIKeyboardWillHideNotification It also provides the animation duration and final keyboard frame. 它还提供了动画持续时间和最终的键盘帧。

- (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;
  }];

}
#define kOFFSET_FOR_KEYBOARD    50.0; //change height for textfield

- (void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    CGRect rect = self.textInputView.frame;
    rect.origin.y = 0.0;
    if (movedUp)
    {
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    }
    if(!movedUp)
    {
        rect.origin.y = 45.0f;
    }
    self.textInputView.frame = rect;
    [UIView commitAnimations];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{

    self setViewMovedUp:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self setViewMovedUp:NO];
}

1.First of all you have to set < UITextFieldDelegate > in your .h file 1.首先您必须在.h文件中设置<UITextFieldDelegate>

2.you have to set the delegate self.textfieldname.delegate = self and then 2.您必须设置代理self.textfieldname.delegate = self然后

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField: textField up: NO];
}

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

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

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

i hope it helps 我希望它会有所帮助

For IOS Swift 对于IOS Swift

func textFieldDidBeginEditing(textField: UITextField) {
        animateTextField(textField, up: true)
    }

func textFieldDidEndEditing(textField: UITextField) {
        animateTextField(textField, up: false)
    }
func animateTextField (textField:UITextField, up:Bool) {

        var movementDistance = 80

        var movementDuration = 1.0
        var movement = CGFloat(up ? -movementDistance : movementDistance)
        UIView.beginAnimations("anim", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration)
        self.view.frame = CGRectOffset(self.view.frame, 0, movement)
        UIView.commitAnimations()
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM