【发布时间】:2017-06-15 09:58:54
【问题描述】:
我有一个内部带有滚动视图的视图控制器,我在情节提要中以这种方式(使用自动布局)设置它:
如您所见,我在滚动视图中的最后一个视图(称为“viewsotto”)中添加了所有对象。 我的问题是: 其中一些对象是文本字段,我希望当我点击它并且键盘显示它可以在文本字段下方时,我可以看到我在其中写的内容。 出于这个原因,我这样做:
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
}
}
}
但它不起作用。我做错了什么?
【问题讨论】:
-
@karthikeyan 是对的。它也很容易实现。
标签: ios swift xcode uiscrollview uitextfield