Christopher
Stoll

iOS Snippet: Using UITextView with the Keyboard

It would seem reasonable to expect that an on-screen keyboard would normally be used with a UITextView, but if the UITextView is full screen then when the keyboard displays the bottom half becomes inaccessible. It is easy enough to fix, but there seem to be more ways to get this wrong than there are to get it right. Below is the method that I have found (from examples on the Apple developer site) to work the best, so far.

- (void)keyboardWillShow:(NSNotification *)notification {
	/*
	 Reduce the size of the text view so
	 that it's not obscured by the keyboard.
	 Animate the resize so that it's in sync
	 with the appearance of the keyboard.
	 */
	
	NSDictionary *userInfo = [notification userInfo];
	
	// Get the origin of the keyboard when it's displayed.
	NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
	
	// Get the top of the keyboard as the y
	// coordinate of its origin in self's view's
	// coordinate system. The bottom of the text
	// view's frame should align with the top
	// of the keyboard's final position.
	//
	CGRect keyboardRect = [aValue CGRectValue];
	keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
	
	CGFloat keyboardTop = keyboardRect.origin.y;
	CGRect newTextViewFrame = self.view.bounds;
	newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
	
	// Get the duration of the animation.
	NSValue *animationDurationValue = [userInfo objectForKey:
		UIKeyboardAnimationDurationUserInfoKey];
	NSTimeInterval animationDuration;
	[animationDurationValue getValue:&animationDuration];
	
	// Animate the resize of the text view's
	// frame in sync with the keyboard's appearance.
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:animationDuration];
	
	self.markShowContent.frame = newTextViewFrame;
	
	[UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notification {
	/*
	 Restore the size of the text view (fill self's view).
	 Animate the resize so that it's in sync with the
	 disappearance of the keyboard.
	 */

	NSDictionary *userInfo = [notification userInfo];

	NSValue *animationDurationValue = [userInfo objectForKey:
		UIKeyboardAnimationDurationUserInfoKey];
	NSTimeInterval animationDuration;
	[animationDurationValue getValue:&animationDuration];
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:animationDuration];
	
	self.markShowContent.frame = self.view.bounds;
	
	[UIView commitAnimations];
}

- (void)setUpKeyboardNotificationHandlers {
	NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
	[center addObserver:self selector:@selector(keyboardWillShow:)
		name:UIKeyboardWillShowNotification object:nil];
	[center addObserver:self selector:@selector(keyboardWillHide:)
		name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
	[self setUpKeyboardNotificationHandlers];
}
Published: 2013-11-13
BloggeriOS 7iOSCodeSnippet