我已经Autolayout
使用了这个屏幕截图。我希望当我点击textView
时,textView将始终位于键盘上方,我也使用自定义NavigationBar
。我已经使用过{{1}它正在工作,但我的IQKeyBoardManagerSwift
也向上移动我想让我的NavigationBar
坚持到顶部,如果我点击textView.Any解决方案。提前谢谢
答案 0 :(得分:2)
Swift 3.0 : - 将UITextView
拖到contentView(UIView)
,创建内容查询底部约束IBOutlet
,bottomConstraint
。使用下面提到的代码后,自定义NavigationBar
也会粘在顶部,只有textView会在键盘上方。
override func viewDidLoad() {
super.viewDidLoad()
let center: NotificationCenter = NotificationCenter.default
center.addObserver(self, selector: #selector(ViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
center.addObserver(self, selector: #selector(ViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification){
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardSizeNow:CGSize = (userInfo.object(forKey: UIKeyboardFrameEndUserInfoKey)! as AnyObject).cgRectValue.size
UIView.animate(withDuration: 0.2, animations: { () -> Void in
self.bottomConstraint.constant = keyboardSizeNow.height - 49
self.view.layoutIfNeeded()
})
}
func keyboardWillHide(notification: NSNotification){
UIView.animate(withDuration: 0.2, animations: { () -> Void in
self.bottomConstraint.constant = 0
self.view.layoutIfNeeded()
})
}
答案 1 :(得分:0)
你可以用类似的方式实现keyboardWillShow和keyboardWillHide方法
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
buttonBottomConstraint.constant = keyboardSize.height
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})
}
}
func keyboardWillHide(notification: NSNotification) {
if let _ = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
bottomConstraint.constant = 0
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})
}
}
另外,请不要忘记在viewDidLoad中观察。