如何在Swift 3+中调整键盘的滚动视图 [英] How to adjust your scrollview for the keyboard in swift 3+
问题描述
如何调整滚动视图以垂直补偿键盘?继续阅读...
How do you adjust your scrollview to compensate for a keyboard vertically? Read on...
是的,我知道这是一些基本信息,但是我今天随机注意到,我看到的有关该主题的所有答案到处都是信息,版本和/或到处都是刘海……但什么都没有对于Swift 3+来说是可靠的.
Yes I know this is some basic info, but I randomly noticed today that all of the answers I saw about this topic are all over the place with info, versions and/or use bangs all over the place... but nothing solid for Swift 3+.
推荐答案
Swift 4.2:
用 scrollView 代替UITableView,UICollectionView等.
Substitute scrollView for UITableView, UICollectionView, etc.
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)
}
添加一些功能来监听通知:
Add some functions to listen for the notifications:
@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文档以获取更多信息.
Worth noting is that if your deployment target is iOS 9 or greater, you don't need to remove the observer anymore. Check the NotificationCenter docs for more info.
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)
}
添加一些功能来监听通知:
Add some functions to listen for the notifications:
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文档以获取更多信息.
Worth noting is that if your deployment target is iOS 9 or greater, you don't need to remove the observer anymore. Check the NotificationCenter docs for more info.
deinit {
NotificationCenter.default.removeObserver(self)
}
这篇关于如何在Swift 3+中调整键盘的滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!