AutoLayout问题Swift 3.0 [英] AutoLayout issue Swift 3.0
问题描述
我已经使用过Autolayout
此屏幕截图.我想当我单击textView
时,textView将始终位于键盘上方,并且我正在使用自定义NavigationBar
.我已经使用过IQKeyBoardManagerSwift
,它可以工作,但是我的NavigationBar
也向上移动,如果我单击textView,我希望我的NavigationBar
停留在顶部.预先感谢
I have already Autolayout
this screenshot using.I want when i click textView
,textView will always just above Keyboard and also i am using custom NavigationBar
.I already used IQKeyBoardManagerSwift
It is working but my NavigationBar
also moves up I want my NavigationBar
to be stick at top if i click textView.Any Solutions to this. thanks in advance
推荐答案
Swift 5.0 :-将UITextView
拖动到contentView(UIView)
中,创建contentView底部约束的IBOutlet
,即bottomConstraint
.使用上述代码后,自定义NavigationBar
也将停留在顶部,只有textView会位于键盘上方.
Swift 5.0 :- Drag your UITextView
in a contentView(UIView)
, Create IBOutlet
of bottom constraint of contentView i.e bottomConstraint
. After use the below code as mentioned and custom NavigationBar
will also stick at top only textView will be just above keyboard.
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()
})
}
这篇关于AutoLayout问题Swift 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!