iOS 键盘弹出页面上移

就是通过系统的一个通知,获取到键盘即将展示,和即将消失的时机,并通过
通知获取到键盘的高度和键盘弹出的duration, 我们上移页面frame的时候也用这个
duration,就能产生和键盘同步移动的效果,下面是代码

添加和移除通知监听

- (void)addKeyboardObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillDisappear:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

- (void)removeKeyboardObserver {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

在监听方法中修改视图的frame

- (void)keyboardWillShow:(NSNotification *)noti
{
    CGRect keyboardRect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat boardHeight = keyboardRect.size.height;
    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    LIVWeakify(self);
    [UIView animateWithDuration:duration animations:^{
        LIVStrongify(self);
        self.viewContent.y -= boardHeight;
    }];
    [self.viewContent addGestureRecognizer:self.endEditingTap];
}

- (void)keyboardWillDisappear:(NSNotification *)noti
{
    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    LIVWeakify(self);
    [UIView animateWithDuration:duration animations:^{
        LIVStrongify(self);
        self.viewContent.y = self.view.height * 0.25;
    }];
    [self.viewContent removeGestureRecognizer:self.endEditingTap];
}
iOS设备上,当软键盘弹出时,有时可能会导致整个页面向上滚动,这通常是因为设置了自动调整视图布局(Auto Layout)的行为,尤其是当有输入框处于顶部并且设置了`adjustsScrollViewInsets`属性为`true`时。当用户聚焦于输入框,系统会默认将键盘占用的空间从视图控制器的视图中减去,导致页面上移。 要解决这个问题,你可以采取以下几种策略: 1. **关闭自动调整**:在你的`ViewController`的初始化方法中,设置`prefersStatusBarHidden`为`true`并自定义管理状态栏来控制是否隐藏,同时关闭`adjustsScrollViewInsets`: ```swift override var prefersStatusBarHidden: Bool { return true } override var automaticallyAdjustsScrollViewInsets: Bool { return false } ``` 2. **手动处理键盘事件**:你可以监听`UIResponder.keyboardWillShowNotification`和`UIResponder.keyboardWillHideNotification`通知,然后相应地调整视图的位置: ```swift NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange), name: UIResponder.keyboardWillChangeFrameNotification, object: nil) @objc func keyboardWillChange() { guard let keyboardFrame = UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets.bottom else { return } // 根据键盘高度调整内容区域位置 self.scrollView.contentInset.top += keyboardFrame self.scrollView.scrollIndicatorInsets.top += keyboardFrame // 可能需要更新约束 layoutIfNeeded() } ``` 3. **使用`PresentationController`**:如果你使用的是SwiftUI,并且想要更现代的方式处理,可以使用`PresentationMode`来管理键盘的显示,而不是直接改变视图的`contentInset`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值