首页 > 代码库 > 弹出键盘,输入框上移问题 iOS

弹出键盘,输入框上移问题 iOS

 viewDidLoad 中添加监听

 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];

  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardWillHideNotification object:nil];

 

实现两个监听方法

- (void)keyboardShow:(NSNotification *)notification{

    if(currentTextField.tag >= 1000){

          CGSize size = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;//键盘大小

        CGFloat keyBoardHeight = size.height+10;//键盘高度,富余10点高度

 

        // screenHeight 手机屏幕总高度, 64是导航栏的高度, currentTextField.bottomY 当前textField的底部位置,

           CGFloat shouldMoveHeight = keyBoardHeight-( screenHeight - 64-currentTextField.bottomY);

        if(shouldMoveHeight > 0){

            _bgScrollView.contentOffset = CGPointMake(0, shouldMoveHeight);

        }

    }

}

- (void)keyboardHidden:(NSNotification *)notification{

    _bgScrollView.contentOffset = CGPointMake(0, 0);

}

 

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

    

}

 

弹出键盘,输入框上移问题 iOS