自动布局问题 Swift 3.0
AutoLayout issue Swift 3.0
我已经 Autolayout
这个屏幕截图 using.I 当我单击 textView
时,textView 将始终位于键盘上方,而且我正在使用自定义 NavigationBar
。我已经使用 IQKeyBoardManagerSwift
它正在工作,但我的 NavigationBar
也向上移动 我希望我的 NavigationBar
在我单击 textView.Any 时保持在顶部 对此的解决方案。提前致谢
Swift 5.0 :- 将您的 UITextView
拖到 contentView(UIView)
中,创建 contentView 底部约束的 IBOutlet
即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()
})
}
您可以用类似的方式实现 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中观察
我已经 Autolayout
这个屏幕截图 using.I 当我单击 textView
时,textView 将始终位于键盘上方,而且我正在使用自定义 NavigationBar
。我已经使用 IQKeyBoardManagerSwift
它正在工作,但我的 NavigationBar
也向上移动 我希望我的 NavigationBar
在我单击 textView.Any 时保持在顶部 对此的解决方案。提前致谢
Swift 5.0 :- 将您的 UITextView
拖到 contentView(UIView)
中,创建 contentView 底部约束的 IBOutlet
即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()
})
}
您可以用类似的方式实现 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中观察