我有滚动视图视图控制器内,我在故事板设置它以这种方式(带自动版式):滚动视图和键盘问题斯威夫特
正如你可以看到我添加的所有对象最后一个视图(称为'viewsotto')在滚动视图中。 我的问题是: 这些对象中的一些是textfield,我希望当我点击它和键盘显示它可以在文本框下方,以便我可以看到我写入的内容。 为此,我做了这种方式:
NotificationCenter.default.addObserver(self, selector: #selector(userProfiloGiusto.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(userProfiloGiusto.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
}
}
,但它不工作。我究竟做错了什么?
检查无需编码https://github.com/hackiftekhar/IQKeyboardManager – karthikeyan
@karthikeyan是对的。它也容易实现。 – vaibby
谢谢。太奇妙了!! –