【发布时间】:2016-06-21 01:24:46
【问题描述】:
所以我有以下代码:
//MARK: - Textfield delegate Methods
func textFieldDidBeginEditing(textField: UITextField)
{
// update view based on updates in the constraints
self.view.layoutIfNeeded()
//perform animation to grow the dockview
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.dockViewHeightConstraint.constant = self.keyBoardHeight + 60
self.view.layoutIfNeeded()
}, completion: nil)
}
和
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
keyBoardHeight = contentInsets.bottom
}
}
我想要做的是以下。当用户单击文本字段时,键盘应该打开,并且我制作的视图应该向上移动。
我通过 NSNotification 触发的keyboardWillShow 函数从键盘获取高度。
问题如下:当我运行代码时,textFieldDidBeginEditing 在 NSNotification 之前被调用,使我的 self.keyBoardHeight 值为 0(初始值)。有没有办法在按下 textField 之前调用 NSNotification 部分?
【问题讨论】:
-
你必须使用 TPKeyboardAvoiding 第三方库
-
只给出一个更好的初始猜测,而不是 0。
-
@Birendra 谢谢,但应该有不同的方法来解决这个问题,而不是在整个第三方库中复制
标签: ios swift keyboard uitextfield nsnotificationcenter