Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we make keyboard appear below textView in swift?

I have done keyboard appearing below textfield using

on View did Load adding a observer()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Gold_Loan_First_ViewController.keyboardDidShow(_:)), name: UIKeyboardDidShowNotification, object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Gold_Loan_First_ViewController.keyboardWillBeHidden(_:)), name: UIKeyboardWillHideNotification, object: nil)

And then updating the frame

weak var activeField: UITextField?

func textFieldDidEndEditing(textField: UITextField) {
    self.activeField = nil


}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if textField==txtOTP {
        txtOTP.errorMessage=""
    }
  return true
}
func textFieldDidBeginEditing(textField: UITextField) {
    self.activeField = textField

}




func keyboardDidShow(notification: NSNotification)
{
    if let activeField = self.activeField,
        let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize.height, right: 0.0)
        self.scrollView.contentInset = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets
        var aRect = self.view.frame
        aRect.size.height -= keyboardSize.size.height
        if (!CGRectContainsPoint(aRect, activeField.frame.origin)) {
            self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
        }
    }


}

func keyboardWillBeHidden(notification: NSNotification)
{
    let contentInsets = UIEdgeInsetsZero
    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
}

But how do I do it for a textView. I tried the same code with didBeginEditing of textView with no positive effect

like image 862
Jeesson_7 Avatar asked Nov 07 '17 06:11

Jeesson_7


People also ask

How do I enable the keyboard in Swift?

To enable SwiftKey and make it your default keyboard on stock Android, head to settings, select Language & input, and choose SwiftKey from the list of options.

How do I make the keyboard go away in swift?

Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.

How do I find the size of the keyboard in Swift?

To configure the size of the keyboard, you need to open the SwiftKey app, then tap “Layout & keys”. Tap “Layout & keys” in the SwiftKey app to get to the keyboard resizing settings. Once in the layout and keys menu, tap the top option “Resize”, to configure the size of your keyboard.


1 Answers

One of the easy and no code of line solution is to use the following pods in your app.

IQKeyboardManger

Later you need to just import that in App Delegate and add this two lines of code in didfinishLaunching method:

  IQKeyboardManager.sharedManager().enable = true

Your problem will be solved for whole app.

For Swift 5:

IQKeyboardManager.shared.enable = true
like image 136
Sucharu Hasija Avatar answered Sep 28 '22 19:09

Sucharu Hasija