我已经使用了自动布局
此屏幕截图。我希望当我单击textView
时,textView将始终位于键盘上方,而且我正在使用自定义NavigationBar
。我已经使用了 IQKeyBoardManagerSwift
它正在工作,但我的 NavigationBar
也向上移动,如果我单击 textView,我希望我的 NavigationBar
停留在顶部.任何解决方案。提前致谢
最佳答案
Swift 5.0 :- 将您的UITextView
拖到contentView(UIView)
中,创建底部约束的IBOutlet
contentView 的,即 bottomConstraint
。使用下面提到的代码后,自定义 NavigationBar
也将粘在顶部,只有 textView 将位于键盘上方。
override func viewDidLoad() {
super.viewDidLoad()
let center: NotificationCenter = NotificationCenter.default
center.addObserver(self, selector: #selector(Profile.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
center.addObserver(self, selector: #selector(Profile.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc 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()
})
}
@objc func keyboardWillHide(notification: NSNotification){
UIView.animate(withDuration: 0.2, animations: { () -> Void in
self.bottomConstraint.constant = 0
self.view.layoutIfNeeded()
})
}
关于swift3 - 自动布局问题 Swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49670145/