2

enter image description here

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 IQKeyBoardManagerSwiftIt is working but my NavigationBar also moves up I want my NavigationBarto be stick at top if i click textView.Any Solutions to this. thanks in advance

Dilip Tiwari
  • 1,441
  • 18
  • 31
  • for message scrolling have you used scrollview ? or your custom Navigation bar is in scrollview or in view ? – CodeChanger Apr 05 '18 at 10:36
  • i also tried my all view except custom navigation in scrollview but still auto layout is not proper @CodeChanger could u help me with this – anuj tiwari Apr 05 '18 at 10:39
  • i m showing messages on tableview from json as well as when i enter text to textview it adds to tableview from api my custom Navigation bar is in View – anuj tiwari Apr 05 '18 at 10:40
  • @anujtiwari I think u should disable iqmanger in this controller and use constraint something like this https://stackoverflow.com/questions/31356293/uitableview-and-uiview-with-keyboardwillshow/31356527#31356527 – Bhavin Bhadani Apr 05 '18 at 12:39

2 Answers2

2

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()
    })
}
Dilip Tiwari
  • 1,441
  • 18
  • 31
0

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.