Swift新手求助:键盘弹出时如何调整PopUp尺寸避免被遮挡
Hey there! As a fellow Swift beginner, I totally get how frustrating it is when the keyboard pops up and blocks your popup list. Let me walk you through a couple of reliable solutions to fix this—both are easy to implement and work great for most scenarios.
方法1:使用NotificationCenter监听键盘通知(最常用方案)
This is the classic go-to approach for handling keyboard occlusion in iOS development. The core idea is to listen for keyboard show/hide events, then dynamically adjust your popup's layout constraints to avoid being blocked.
步骤1:注册键盘通知监听
Add these lines in your ViewController's viewDidLoad() method to start listening for keyboard events:
override func viewDidLoad() { super.viewDidLoad() // 监听键盘弹出事件 NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil ) // 监听键盘收起事件 NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil ) }
Don't forget to remove the observer when the view controller is deallocated to avoid memory leaks:
deinit { NotificationCenter.default.removeObserver(self) }
步骤2:处理键盘事件,调整PopUp布局
Assume your custom popup has a bottom constraint (named popupBottomConstraint) that controls its distance from the parent view's bottom. We'll modify this constraint's constant value when the keyboard appears:
@objc private func keyboardWillShow(_ notification: Notification) { // 从通知信息中获取键盘高度和动画时长 guard let userInfo = notification.userInfo, let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect, let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else { return } let keyboardHeight = keyboardFrame.height // 调整底部约束,让PopUp向上移动键盘的高度 popupBottomConstraint.constant = keyboardHeight // 跟随键盘的动画节奏更新布局,让过渡更流畅 UIView.animate(withDuration: animationDuration) { self.view.layoutIfNeeded() } } @objc private func keyboardWillHide(_ notification: Notification) { guard let animationDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else { return } // 键盘收起时,恢复底部约束的原始值(比如0) popupBottomConstraint.constant = 0 UIView.animate(withDuration: animationDuration) { self.view.layoutIfNeeded() } }
小提示
- If you're using
UIAlertControlleras your popup, the system already handles keyboard occlusion automatically. This method is mainly for custom popups built withUIView. - Make sure your popup uses Auto Layout properly—this ensures modifying constraints will actually adjust the popup's position.
方法2:用UIScrollView包裹PopUp内容(适合长列表场景)
If your popup contains a long list (like UITableView or UICollectionView), wrapping the popup's content in a UIScrollView is a great option. Users can scroll to view blocked content, and you can adjust the scroll view's inset to match the keyboard height.
核心代码示例
@objc private func keyboardWillShow(_ notification: Notification) { guard let userInfo = notification.userInfo, let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return } let keyboardHeight = keyboardFrame.height // 给滚动视图添加底部内边距,适配键盘高度 popupScrollView.contentInset.bottom = keyboardHeight popupScrollView.scrollIndicatorInsets.bottom = keyboardHeight } @objc private func keyboardWillHide(_ notification: Notification) { // 键盘收起时恢复内边距 popupScrollView.contentInset.bottom = 0 popupScrollView.scrollIndicatorInsets.bottom = 0 }
This approach works well when you don't want to resize the popup itself, but still want users to access all content when the keyboard is present.
额外注意事项
- If you're working with SwiftUI, the implementation will be different, but since you mentioned a popup in an interface, these UIKit-focused solutions should fit your needs.
- Test on different device sizes (e.g., iPhone SE and iPhone 14 Pro) to ensure the popup adjusts correctly across all screens.
内容的提问来源于stack exchange,提问作者Junior Sanches

