How can we make the keyboard below textView in swift?

I made a keyboard appearing below the text box using

on View did Download Add Watcher ()

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 frame update

 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 to do it for textView. I tried the same code with didBeginEditing from textView without a positive effect

+5
source share
5 answers

One simple and straightforward linear solution is to use the following modules in your application.

IQKeyboardManger

Later, you just need to import this into the App Delegate and add these two lines of code to the dofinishLaunching method:

  IQKeyboardManager.sharedManager().enable = true 

Your problem will be solved for the entire application.

+2
source

I survived such a situation. To do this, I added an extension to my UiViewController :

  extension CCViewController : UITextViewDelegate { func textViewDidBeginEditing(_ textView: UITextView) { // write code as per your requirements } func textViewDidEndEditing(_ textView: UITextView) { // write code as per your requirements } func textViewDidChange(_ textView: UITextView) { self.p_adjustMessageFieldFrameForTextView(textView) } } // Defining the p_adjustMessageFieldFrameForTextView method fileprivate func p_adjustMessageFieldFrameForTextView(_ textView : UITextView) { var finalheight : CGFloat = textView.contentSize.height + 10 var kMaxMessageFieldHeight : CGFloat = 50 if (finalheight > kMaxMessageFieldHeight) { finalheight = kMaxMessageFieldHeight; } else if (finalheight < kCommentTextViewHeight){ finalheight = kCommentTextViewHeight; } scrollView!.view.frame = CGRect(x: 0, y: kNavBarHeight + statuBarHeight, width: SCREEN_WIDTH,height: SCREEN_HEIGHT - finalheight - keyboardRect!.size.height - kNavBarHeight - statuBarHeight) // It is there for understanding that we have to calculate the exact frame of scroll view commentTextView!.frame = CGRect(x: 0, y: bottomOfView(scrollView!.view), width: SCREEN_WIDTH, height: finalheight) self.p_setContentOffsetWhenKeyboardIsVisible() } // Defining the p_setContentOffsetWhenKeyboardIsVisible method fileprivate func p_setContentOffsetWhenKeyboardIsVisible() { // here you can set the offset of your scroll view } 

There is also a method called bottomView() :

 func bottomOfView(_ view : UIView) -> CGFloat { return view.frame.origin.y + view.frame.size.height } 
+1
source

Salman Gumsani is right.

Change UIKeyboardFrameBeginUserInfoKey to UIKeyboardFrameEndUserInfoKey .

Apple Debeloper Document: Keyboard Notification Keys User Information

UIKeyboardFrameEndUserInfoKey

A key for an NSValue object containing a CGRect that identifies the rectangle of the ending frame of the keyboard in screen coordinates. The box's rectangle reflects the current orientation of the device.

UIKeyboardFrameBeginUserInfoKey

The key is for an NSValue object containing a CGRect that identifies the rectangle of the starting frame of the keyboard in screen coordinates. The box's rectangle reflects the current orientation of the device.

Conclusion

So, if u uses UIKeyboardFrameBeginUserInfoKey , you can increase the keyboard height to 0 . The UITextView thought UITextView not covered.

+1
source

Find the correct keyboard height and assign it to the lower limit of the textView or decrease the y-position of the textView. For instance:

Step 1: make the keyboardHeight property in your viewController.

var keyboardHeight: CGFloat = 0.0

Step-2: Make the @IBOutlet lower bound the textView.

@IBOutlet weak var textViewBottomConstraint: NSLayoutConstraint!

Step 3:

 fileprivate func addKeyboardNotification() { NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } fileprivate func removeKeyboardNotification() { IQKeyboardManager.shared().isEnabled = true NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) } 

Copy and paste these functions into your view controllers and call self.addKeyboardNotification() in viewDidLoad() , and you - viewController

Step 4:

 deinit { self.removeKeyboardNotification() } 

also add this code to your viewController.

Step 5:

  func keyboardWillShow(_ notification: Notification) { if let keboardFrame = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue, self.keyboardHeight <= 0.0 { self.keyboardHeight = keboardFrame.height + 45.0 //(Add 45 if your keyboard have toolBar if not then remove it) } UIView.animate(withDuration: 0.3, animations: { self.textViewBottomConstraint.constant = self.keyboardHeight }, completion: { (success) in }) } func keyboardWillHide(_ notification: Notification) { UIView.animate(withDuration: 0.3, animations: { self.textViewBottomConstraint.constant = 0.0 }, completion: { (success) in }) } 

So that he controls the textView using the keyboard. If you do not want to use textViewBottomConstraint , you can work with the y-position of textView.

+1
source

You can simply use TPKAScrollViewController.h and TPKAScrollViewController.m using the Bridging Header header.

When you drag these objective-C files into your quick project, it will automatically ask for Create Bridging. Create a title for the title and import #import "TPKAScrollViewController.h" into the YourApp-Bridging-Header.h file.

enter image description here

After that, just select your scrollView in XIB and change its class to TPKeyboardAvoidingScrollView , as shown below.

enter image description here

+1
source

Source: https://habr.com/ru/post/1273160/


All Articles