iOS 8 UIView not moving up when keyboard appears

后端 未结 4 1715
甜味超标
甜味超标 2021-02-04 12:32

I am developing a chat app which has UITableView and a UIView containing a UITextField and a UIButton in it. I am using the f

4条回答
  •  忘了有多久
    2021-02-04 13:13

    Your code seems to be correct but i will prefer using UIKeyboardDidChangeFrameNotification or UIKeyboardWillChangeFrameNotification because these will tell you the change in keyboard frame when predictive text bar gets up or down when keyboard is in view.

    In your ViewDidLoad add this

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardFrameDidChange:)
                                                 name:UIKeyboardDidChangeFrameNotification object:nil];
    

    and then paste this method in your ViewController

    -(void)keyboardFrameDidChange:(NSNotification*)notification{
        NSDictionary* info = [notification userInfo];
    
        CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        [yourView setFrame:CGRectMake(0, kKeyBoardFrame.origin.y-yourView.frame.size.height, 320, yourView.frame.size.height)];
    }
    

    This will handle all your keyboard cases like when its up or down or change in its frame with predictive text bar

    and also remove observer when you are leaving your view

提交回复
热议问题