UIModalPresentationFormSheet on iPad. How to adjust UITextView height when keyboard appears

UITextView is a subview of a modal controller. I need to reduce the height of the UITextView when the keyboard appears so that the coordinate of the lower border y of the UITextView is equal to the coordinate of the upper axis of the keyboard. I'getting height height

CGRect frameBegin = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] ; CGRect frameEnd = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect resultBegin = [self.view convertRect:frameBegin fromView:nil]; CGRect resultEnd = [self.view convertRect:frameEnd fromView:nil]; CGFloat kbdHeight = resultBegin.origin.y - resultEnd.origin.y; 

The problem is that this modal view jumps up when the keyboard appears. How to calculate the coordinate of the upper border of the keyboard in this case?

+7
source share
2 answers

You can do it:

 1. Register for keyboard notifications: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myTextViewHeightAdjustMethod:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myTextViewHeightAdjustMethod:) name:UIKeyboardDidShowNotification object:nil]; 2. Calculate intersection and adjust textView height with the bottom constraint - (void)myTextViewHeightAdjustMethod:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; CGRect keyboardFinalFrame = [[userInfo emf_ObjectOrNilForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGPoint keyboardOriginInView = [self.view convertPoint:keyboardFinalFrame.origin fromView:nil]; CGFloat intersectionY = CGRectGetMaxY(self.view.frame) - keyboardOriginInView.y; if (intersectionY >= 0) { self.textViewBottomConstraint.constant = intersectionY + originalTextViewBottomConstraint; [self.textView setNeedsLayout]; } 

Remember to unregister for notifications.

+4
source

If you do not need to write code for this, I recommend using https://github.com/hackiftekhar/IQKeyboardManager

It works fine (so far for me), and all you have to do is import it and add this one line of code to AppDelegate:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { //Magic one-liner IQKeyboardManager.sharedManager().enable = true return true } 
0
source

All Articles