【发布时间】:2016-08-06 10:52:13
【问题描述】:
我试图让UItextView 的底部在出现键盘时更改约束,因此UItextView 受到textView 的“约束”。我有一个UIView 我在UItextView 和底部布局指南之间命名为spacer。我正在尝试将视图底部的垫片的约束值更改为键盘高度。任何帮助表示赞赏。
这是我的代码:
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var spacer: UIView!
@IBOutlet weak var spacerBottomLayoutConstraint: NSLayoutConstraint!
在viewDidLoad:
textView.becomeFirstResponder()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AdditionalDetailsVC.keyboardShown(_:)), name: UIKeyboardDidShowNotification, object: nil)
魔法发生的地方:
func keyboardShown(notification: NSNotification) {
let info = notification.userInfo!
let value: AnyObject = info[UIKeyboardFrameEndUserInfoKey]!
let rawFrame = value.CGRectValue
keyboardFrame = view.convertRect(rawFrame, fromView: nil)
print(keyboardFrame)
print(spacerBottomLayoutConstraint)
//Setting text field to bottom of keyboard
//spacerBottomLayoutConstraint.constant = keyboardFrame.height
UIView.animateWithDuration(1, animations: {
self.spacerBottomLayoutConstraint.constant = self.keyboardFrame.height
self.view.layoutIfNeeded()
})
}
【问题讨论】:
-
还有什么问题?我看到了你的代码,我看到了你的目标,但我没有看到任何问题。 (顺便说一句,您不需要动画;您可以与键盘一起获得自动动画。)
-
我希望键盘推动
UITextView,并在两者之间保留UIView间隔的缓冲区。现在文本字段在键盘后面。
标签: ios swift autolayout uitextview uikeyboard