Swift iOS 键盘弹出时如何防止自定义导航栏上移 仅滚动tableView与textView
问题根因
- 系统默认的键盘避让机制会自动调整根视图的frame、安全区边距,所有绑定在根视图上的子视图(含自定义导航栏)都会跟着整体上移
- 现有键盘监听逻辑仅单独修改了tableView的内容边距,没有联动调整输入区textView的位置,未对不需要移动的导航栏做位置锁止
- 若使用过修改整个根视图transform/frame的老式键盘适配写法,会直接带着所有子视图(含导航栏)同步上移
修复实现步骤
1. 固定导航栏布局位置
从约束层面锁止导航栏位置,全程不随键盘事件修改:
- 导航栏顶部约束绑定到根视图的安全区顶部
- 导航栏左右边距绑定到根视图的左右边距
- 导航栏设置固定高度值
- 所有键盘事件回调中,不修改导航栏的frame、transform、约束常量,保证它始终固定在顶部原始位置
2. 重构视图层级的约束绑定关系
将非导航栏的视图调整为联动绑定,避免单独修改约束带来的错位问题:
- tableView顶部绑定到导航栏的底部
- tableView左右边距绑定到根视图的左右边距
- tableView底部绑定到textView的顶部
- textView左右边距绑定到根视图的左右边距
- textView设置固定高度
- 为textView的底部约束拖线生成可动态修改的约束变量(例:
textViewBottomConstraint),初始值设为0,即紧贴根视图安全区底部
3. 重写键盘监听逻辑
替换现有仅修改tableView contentInset的实现,监听逻辑同时处理textView位置调整、tableView边距重置:
private var keyboardObserver: NSObjectProtocol? private var textViewBottomConstraint: NSLayoutConstraint! func registerKeyboardObservers() { // 监听键盘弹出事件 keyboardObserver = NotificationCenter.default.addObserver( forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main ) { [weak self] notification in guard let self = self, let userInfo = notification.userInfo, let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect, let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double, let animationCurve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt else { return } // 将键盘全局frame转换为当前视图的本地坐标系 let keyboardFrameInView = self.view.convert(keyboardFrame, from: nil) let bottomSafeArea = self.view.safeAreaInsets.bottom let offsetHeight = keyboardFrameInView.height - bottomSafeArea // 抬升textView到键盘上方 self.textViewBottomConstraint.constant = -offsetHeight // 重置tableView内容边距,避免双重偏移 self.tableView.contentInset = .zero self.tableView.scrollIndicatorInsets = .zero // 匹配键盘动画参数,保证切换流畅一致 UIView.animate( withDuration: animationDuration, delay: 0, options: UIView.AnimationOptions(rawValue: animationCurve << 16) ) { self.view.layoutIfNeeded() } } // 监听键盘收起事件 NotificationCenter.default.addObserver( forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main ) { [weak self] notification in guard let self = self, let userInfo = notification.userInfo, let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double, let animationCurve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt else { return } // 恢复textView初始位置 self.textViewBottomConstraint.constant = 0 self.tableView.contentInset = .zero self.tableView.scrollIndicatorInsets = .zero UIView.animate( withDuration: animationDuration, delay: 0, options: UIView.AnimationOptions(rawValue: animationCurve << 16) ) { self.view.layoutIfNeeded() } } } // 页面销毁时移除监听,避免内存泄漏 deinit { if let observer = keyboardObserver { NotificationCenter.default.removeObserver(observer) } }
4. 禁用会导致全局视图偏移的系统默认配置
在viewDidLoad中添加配置,阻止系统自动将整个根视图向上推:
override func viewDidLoad() { super.viewDidLoad() // 禁用系统自动调整滚动视图内容边距的默认行为 if #available(iOS 11.0, *) { self.tableView.contentInsetAdjustmentBehavior = .never } else { self.automaticallyAdjustsScrollViewInsets = false } }
注意:不要使用修改整个根视图transform、修改根视图frame的老式写法适配键盘,这类写法必然会带着导航栏一起上移,完全不符合需求。原有的两个仅修改scrollView contentInset的键盘监听扩展方法可直接移除,避免新旧逻辑同时执行导致的双重偏移。
内容的提问来源于stack exchange,提问作者Manojkanth
相关产品推荐
相关产品推荐

