How to make my UITextView screen height minus keyboard under iOS 7?

My code worked fine under iOS 6, but under iOS 7 I canโ€™t get my UITextView to enlarge the screen of the device minus the keyboard (in other words, when the keyboard is up, UITextView will still be in full screen mode, but not under the keyboard).

Firstly, when I put a UITextView in my view controller (which is built into the navigation controller), it should also be below the navigation bar, otherwise it will start too far down.

From there, I tried all of these examples:

self.textView.contentInset = UIEdgeInsetsMake(0, 0, 230, 0); self.textView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 230, 0); self.textView.textContainerInset = UIEdgeInsetsMake(0, 0, 230, 0); 

Where in each keyboard text is still visible at some points. I also tried to set a height limit and manipulate the constant, but no luck.

 self.height.constant = self.height.constant - 240.0; 

(Where height is the output of the constraint).

All of them were checked before and after the call [self.textField becomeFirstResponder]; .

How can i do this? I just want a full UITextView screen where the carriage thing will not go under the keyboard, but such basic functionality seems crazy under iOS 7.

+7
ios objective-c ios7 uitextview uiscrollview
source share
2 answers

The โ€œrightโ€ way to do this pre-iOS7 has always been to set the contentInset property of UITextView (a subclass of UIScrollView) when the keyboard shows or hides. I have not personally investigated, but in iOS7 this does not work well, because the bottom insert is either not completed, or there is a problem with the cursor still going below the keyboard. See this question for reference.

In your case, when you use auto-layout, and all you need is a full-screen text view, you can simply set up one restriction when the keyboard shows or hides. This will adjust the height of your text view:

 @implementation TSViewController { IBOutlet NSLayoutConstraint* _textViewSpaceToBottomConstraint; } - (void) dealloc { [[NSNotificationCenter defaultCenter] removeObserver: self]; } - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void) keyboardWillShow: (NSNotification*) n { NSValue* bv = n.userInfo[UIKeyboardFrameEndUserInfoKey]; CGRect br = [bv CGRectValue]; _textViewSpaceToBottomConstraint.constant = br.size.height; } - (void) keyboardWillHide: (NSNotification*) n { _textViewSpaceToBottomConstraint.constant = 0; } @end 

In your storyboard, drop your UITextView into your view-controller view and add 4 constraints to stick its edges to the edges of the view-controller view. Connect the lower space constraint to the IBOutlet _textViewSpaceToBottomConstraint in the view controller.

You can play around with this a bit and adjust the size inside the animation block, borrowing the animation time and the curve from the keyboard notification.

I would be interested to see a version of this that sets the ContentInset and whether it works correctly.

enter image description here

EDIT

Here is another question that addresses this issue with a solution. Obviously, the problem with the carriage (cursor) coming out of the frame is the same problem that I mentioned above re. The "right" way to do this is to set the contentInset property. Thus, the fix for this problem should allow you to simply adjust the contentInset or change the text frame (via .frame or through restriction).

EDIT 2

Last thought about this. In iOS7, the keyboard is translucent. The user should be able to see the contents behind the keyboard. Changing the size of the UITextView above the keyboard will never happen. Thus, the โ€œrightโ€ solution still adjusts the contents of the Inset for the bottom of the text view to add the height of the keyboard to the scrollable text area. Then, in an ideal world, you edit the textOffset of the text view to preserve the caret appearance when the keyboard appears. Finally, add โ€œfixโ€ to this to keep the carriage in the position above the keyboard when it is displayed, but the user enters new lines.

+3
source share

The solution for this in iOS7 is to make the tableview present in the form of a container, which will become your main view. You can add restrictions to it so that it is bound to topLayoutGuide . Add a second note location, which is limited to the bottom of the table at the top and bottomLayoutConstraint at the bottom. Add a constraint to this placeholder view so that its height is 0 and hold ivar on it. You can provide a subclass of UITableViewController that overrides the tableView property using a real table view.

When the keyboard is ready for a kiss, you can get the height of the table from the notification (code below) and animate the setting of the constraint property that looks equivalent to the height of the keyboard.

Keyboard notification code below:

 -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; [defaultCenter addObserver:self selector:@selector(keyboardMoving:) name:UIKeyboardWillShowNotification object:nil]; [defaultCenter addObserver:self selector:@selector(keyboardMoving:) name:UIKeyboardWillHideNotification object:nil]; [defaultCenter addObserver:self selector:@selector(keyboardMoving:) name:UIKeyboardDidHideNotification object:nil]; } -(void)viewWillDisappear:(BOOL)animated { NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; [defaultCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [defaultCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil]; [defaultCenter removeObserver:self name:UIKeyboardDidHideNotification object:nil]; [super viewWillDisappear:animated]; } - (void)keyboardMoving:(NSNotification *)note { NSString *msg = note.name; if([msg isEqualToString:UIKeyboardWillShowNotification] && !_keyboardUp) { _keyboardUp = YES; [[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&_animationDuration]; [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardRect]; NSLog(@"ORIG KEYBOARD %@", NSStringFromCGRect(_keyboardRect)); _keyboardRect = [self.view convertRect:_keyboardRect fromView:nil]; NSLog(@"NEW KEYBOARD %@", NSStringFromCGRect(_keyboardRect)); _animate = YES; } else if([msg isEqualToString:UIKeyboardWillHideNotification] && _keyboardUp) { _keyboardUp = NO; _animate = YES; } else if([msg isEqualToString:UIKeyboardDidHideNotification]) { _keyboardUp = NO; _animate = NO; } } - (BOOL)isKeyboardMovingUp { return _keyboardUp == YES && _animate == YES; } - (BOOL)isKeyboardMovingDown { return _keyboardUp == NO && _animate == YES; } - (BOOL)isKeyboardDown { return _keyboardUp == NO && _animate == NO; } 

What you will need to do before the keyboard appears or simply creates an empty container view and adds it, adds a table to this view, adds

+1
source share

All Articles