Swift3 自动布局问题Swift 3.0
Swift3 自动布局问题Swift 3.0,swift3,autolayout,uinavigationbar,iqkeyboardmanager,Swift3,Autolayout,Uinavigationbar,Iqkeyboardmanager,我已经使用Autolayout此屏幕截图。我想在单击textView时,textView将始终位于键盘上方,并且我正在使用自定义的导航栏。我已经使用了IQKeyBoardManagerSwift它正在工作,但我的导航栏也会向上移动。如果我单击textView,我希望我的导航栏保持在顶部。有什么解决方案吗。提前感谢Swift 5.0:-在contentView(UIView)中拖动UITextView,创建contentView底部约束的IBOutlet,即bottomConstraint。如前所
我已经使用
Autolayout
此屏幕截图。我想在单击textView
时,textView将始终位于键盘上方,并且我正在使用自定义的导航栏
。我已经使用了IQKeyBoardManagerSwift
它正在工作,但我的导航栏
也会向上移动。如果我单击textView,我希望我的导航栏
保持在顶部。有什么解决方案吗。提前感谢Swift 5.0:-在contentView(UIView)
中拖动UITextView
,创建contentView底部约束的IBOutlet
,即bottomConstraint
。如前所述使用以下代码后,自定义导航栏也将粘贴在顶部,只有文本视图将位于键盘上方
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中观察。对于消息滚动,您是否使用了scrollview?或者您的自定义导航栏在scrollview或view中?我也尝试了除scrollview中的自定义导航之外的所有视图,但自动布局仍然不正确@CodeChanger您能帮我吗?我正在从json在tableview上显示消息,以及当我在textview中输入文本时,它从api添加到tableview中我的自定义导航栏在其中View@anujtiwari我认为你应该在这个控制器中禁用iqmanger,并使用类似于这样的约束