问题
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
回答1:
Swift 3.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(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()
})
}
回答2:
you can implement keyboardWillShow and keyboardWillHide method in similar way
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()
})
}
}
also, don't forget to observe in viewDidLoad.
来源:https://stackoverflow.com/questions/49670145/autolayout-issue-swift-3-0