当前位置: 代码迷 >> 综合 >> iOS输入框自适应键盘高度,避免遮挡
  详细解决方案

iOS输入框自适应键盘高度,避免遮挡

热度:48   发布时间:2024-01-20 19:12:49.0

//注册通知

- (void)regNotification

{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

}


- (void)unregNotification

{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];

}


#pragma mark - notification handler

- (void)keyboardWillChangeFrame:(NSNotification *)notification

{

    NSDictionary *info = [notification userInfo];

    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];

    CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];

    CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    

    CGFloat yOffset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y;

    

    CGRect inputFieldRect = self.TextField.frame;

    CGRect moreBtnRect = self.moreTypeBtn.frame;

    

    inputFieldRect.origin.y += yOffset;

    moreBtnRect.origin.y += yOffset;

    

    [UIView animateWithDuration:duration animations:^{

        self.inputTextField.frame = inputFieldRect;

        self.moreInputTypeBtn.frame = moreBtnRect;

    }];

}


  相关解决方案