Click here to Skip to main content
Click here to Skip to main content

iOS Keyboard in Landscape

By , 7 Jan 2012
 
There are a number of articles that describe how to fit your view in the available space when the keyboard is being displayed. So the keyboard does not cover the UIView. But I haven't seen one that covers the situation when the orientation is landscape.
 
The trick here is that although the view is aware of the orientation, the keyboard is not. This means in Landscape, the keyboards width is actually its height and visa versa. So I've posted a number of methods but the keyboardWillShow method is the one of most interest. And in particular, the line
textView.frame = CGRectMake(0, 0, size.width , size.height - keyboardSize.width + 50);
 
Note that the keyboards width property is accessed to recalcuate the textviews height:
 
- (void) viewDidLoad {
 
    [super viewDidLoad];
    //Do stuff

    // Listen to keyboard show and hide events
 	// listen for keyboard
	[[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 *)notification {
	
    CGSize keyboardSize = [[[notification userInfo] 
                          objectForKey:UIKeyboardFrameBeginUserInfoKey] 
                          CGRectValue].size;
    
    UIInterfaceOrientation orientation = 
        [[UIApplication sharedApplication] statusBarOrientation];
    
    CGSize size = self.view.frame.size;
	
    if (orientation == UIDeviceOrientationPortrait 
        || orientation == UIDeviceOrientationPortraitUpsideDown ) {
	textView.frame = CGRectMake(0, 0, 
                                   size.width, 
                                   size.height - keyboardSize.height + 50);
    } else {
	//Note that the keyboard size is not oriented
        //so use width property instead	
	textView.frame = CGRectMake(0, 0, 
                                   size.width, 
                                   size.height - keyboardSize.width + 50);
    }
}
 
- (void) keyboardWillHide:(NSNotification *)notification {
 
    textView.frame = CGRectMake(0, 0, 
                               self.view.frame.size.width, 
                               self.view.frame.size.height + 25);
}
- (void) dealloc {
 
    [[NSNotificationCenter defaultCenter] removeObserver:self];
 
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

RobCroll
Australia Australia
Member
Enjoying life developing mobile device software for Contractors Apps and Ezi App.
 
I also teach C#, Java and Project Management a couple of evenings a week.
 
Prior to moving to DCB, I'd been a Windows software developer for nearly 15 years

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 7 Jan 2012
Article Copyright 2012 by RobCroll
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid