如何在swift 3+中调整键盘的滚动视图

时间:2017-05-24 19:54:17

标签: ios iphone swift swift3

如何调整滚动视图以垂直补偿键盘?请继续阅读...

是的我知道这是一些基本信息,但我今天随机注意到,我看到的关于这个主题的所有答案都到处都是信息,版本和/或使用刘海......但没有适用于Swift 3 +。

2 个答案:

答案 0 :(得分:16)

Swift 4.2:

为UITableView,UICollectionView等替换 scrollView

let scrollView = UIScrollView()

添加观察员。

override open func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}

添加一些功能来收听通知:

@objc func keyboardWillHide(notification: Notification) {
    let contentInsets = UIEdgeInsets.zero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

@objc func keyboardWillShow(notification: Notification) {
    guard let keyboardFrame: CGRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
    scrollView.contentInset.bottom = keyboardFrame.height
}

值得注意的是,如果您的部署目标是iOS 9或更高版本,则无需再删除观察者。有关详细信息,请查看NotificationCenter文档。

deinit {
    NotificationCenter.default.removeObserver(self)
}

----------------------------------------------- -

斯威夫特3:

let scrollView = UIScrollView()

添加观察员。

override open func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(noti:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    NSNotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(noti:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}

添加一些功能来收听通知:

func keyboardWillHide(noti: Notification) {
    let contentInsets = UIEdgeInsets.zero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

func keyboardWillShow(noti: Notification) {

    guard let userInfo = noti.userInfo else { return }
    guard var keyboardFrame: CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else { return }
    keyboardFrame = self.view.convert(keyboardFrame, from: nil)

    var contentInset:UIEdgeInsets = scrollView.contentInset
    contentInset.bottom = keyboardFrame.size.height
    scrollView.contentInset = contentInset
}

值得注意的是,如果您的部署目标是iOS 9或更高版本,则无需再删除观察者。有关详细信息,请查看NotificationCenter文档。

deinit {
    NotificationCenter.default.removeObserver(self)
}

答案 1 :(得分:6)

使其在iOS 11上运行的修改是使用UIKeyboardFrameEndUserInfoKey而不是UIKeyboardFrameBeginUserInfoKey。只是简化@ crewshin解决方案的方法:

@objc func keyboardWillShow(_ notification: NSNotification) {                
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        scrollView.contentInset.bottom = keyboardSize.height
    }
}

@objc func keyboardWillHide(_ notification: NSNotification) {        
    scrollView.contentInset.bottom = 0
}