Skip to content

Commit

Permalink
Implemented support for any interface orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltyson committed Apr 22, 2011
1 parent 2ad5d8a commit 5153814
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 42 deletions.
3 changes: 1 addition & 2 deletions TPKeyboardAvoidingSample/FirstViewController.m
Expand Up @@ -21,8 +21,7 @@ - (void)viewDidLoad

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}


Expand Down
3 changes: 1 addition & 2 deletions TPKeyboardAvoidingSample/SecondViewController.m
Expand Up @@ -75,8 +75,7 @@ - (void)viewDidDisappear:(BOOL)animated

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}

#pragma mark - Table view data source
Expand Down
2 changes: 1 addition & 1 deletion TPKeyboardAvoidingScrollView.h
Expand Up @@ -9,7 +9,7 @@


@interface TPKeyboardAvoidingScrollView : UIScrollView {
BOOL resizedForKeyboard;
CGRect priorFrame;
}

@end
33 changes: 16 additions & 17 deletions TPKeyboardAvoidingScrollView.m
Expand Up @@ -39,29 +39,33 @@ -(void)dealloc {
}

- (void)keyboardWillShow:(NSNotification*)notification {
if ( resizedForKeyboard ) return;
if ( !CGRectEqualToRect(priorFrame, CGRectZero) ) return;

UIView *firstResponder = [self findFirstResponderBeneathView:self];
if ( !firstResponder ) {
// No child view is the first responder - nothing to do here
return;
}

CGRect keyboardBounds = [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue];
if ( keyboardBounds.origin.y == 0 ) keyboardBounds.origin = CGPointMake(0, [UIScreen mainScreen].bounds.size.height - keyboardBounds.size.height);
CGFloat spaceAboveKeyboard = keyboardBounds.origin.y - [self convertRect:self.bounds toView:self.window].origin.y;
priorFrame = self.frame;

// Use this view's coordinate system
CGRect keyboardBounds = [self convertRect:[[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
CGRect screenBounds = [self convertRect:[UIScreen mainScreen].bounds fromView:nil];
if ( keyboardBounds.origin.y == 0 ) keyboardBounds.origin = CGPointMake(0, screenBounds.size.height - keyboardBounds.size.height);

CGFloat spaceAboveKeyboard = keyboardBounds.origin.y - self.bounds.origin.y;
CGFloat offset = -1;

CGRect frameInScreenCoords = [self convertRect:self.bounds toView:nil];
CGRect newFrame = self.frame;
newFrame.size.height -= keyboardBounds.size.height -
((keyboardBounds.origin.y+keyboardBounds.size.height)
- (frameInScreenCoords.origin.y+frameInScreenCoords.size.height));
- (self.bounds.origin.y+self.bounds.size.height));

CGRect rectInScreen = [firstResponder convertRect:firstResponder.bounds toView:nil];
if ( rectInScreen.origin.y + rectInScreen.size.height >= [[UIScreen mainScreen] bounds].size.height - keyboardBounds.size.height ) {
CGRect firstResponderFrame = [firstResponder convertRect:firstResponder.bounds toView:self];
if ( firstResponderFrame.origin.y + firstResponderFrame.size.height >= screenBounds.origin.y + screenBounds.size.height - keyboardBounds.size.height ) {
// Prepare to scroll to make sure the view is above the keyboard
offset = [firstResponder convertRect:firstResponder.bounds toView:self].origin.y + self.contentOffset.y;
offset = firstResponderFrame.origin.y + self.contentOffset.y;
if ( self.contentSize.height - offset < newFrame.size.height ) {
// Scroll to the bottom
offset = self.contentSize.height - newFrame.size.height;
Expand All @@ -87,22 +91,17 @@ - (void)keyboardWillShow:(NSNotification*)notification {
[self setContentOffset:CGPointMake(self.contentOffset.x, offset) animated:YES];
}
[UIView commitAnimations];

resizedForKeyboard = YES;
}

- (void)keyboardWillHide:(NSNotification*)notification {
if ( !resizedForKeyboard ) return;

resizedForKeyboard = NO;
if ( CGRectEqualToRect(priorFrame, CGRectZero) ) return;

// Restore dimensions to prior size
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
CGRect frame = self.frame;
frame.size.height += [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
self.frame = frame;
self.frame = priorFrame;
priorFrame = CGRectZero;
[UIView commitAnimations];
}

Expand Down
2 changes: 1 addition & 1 deletion TPKeyboardAvoidingTableView.h
Expand Up @@ -9,7 +9,7 @@


@interface TPKeyboardAvoidingTableView : UITableView {
BOOL resizedForKeyboard;
CGRect priorFrame;
}

@end
38 changes: 19 additions & 19 deletions TPKeyboardAvoidingTableView.m
Expand Up @@ -35,30 +35,35 @@ -(void)dealloc {
[super dealloc];
}

- (void)keyboardWillShow:(NSNotification*)notification {
if ( resizedForKeyboard ) return;

- (void)keyboardWillShow:(NSNotification*)notification {
if ( !CGRectEqualToRect(priorFrame, CGRectZero) ) return;

UIView *firstResponder = [self findFirstResponderBeneathView:self];
if ( !firstResponder ) {
// No child view is the first responder - nothing to do here
return;
}

CGRect keyboardBounds = [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue];
if ( keyboardBounds.origin.y == 0 ) keyboardBounds.origin = CGPointMake(0, [UIScreen mainScreen].bounds.size.height - keyboardBounds.size.height);
CGFloat spaceAboveKeyboard = keyboardBounds.origin.y - [self convertRect:self.bounds toView:self.window].origin.y;
priorFrame = self.frame;

// Use this view's coordinate system
CGRect keyboardBounds = [self convertRect:[[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
CGRect screenBounds = [self convertRect:[UIScreen mainScreen].bounds fromView:nil];
if ( keyboardBounds.origin.y == 0 ) keyboardBounds.origin = CGPointMake(0, screenBounds.size.height - keyboardBounds.size.height);

CGFloat spaceAboveKeyboard = keyboardBounds.origin.y - self.bounds.origin.y;
CGFloat offset = -1;

CGRect frameInScreenCoords = [self convertRect:self.bounds toView:nil];
CGRect newFrame = self.frame;
newFrame.size.height -= keyboardBounds.size.height -
((keyboardBounds.origin.y+keyboardBounds.size.height)
- (frameInScreenCoords.origin.y+frameInScreenCoords.size.height));
((keyboardBounds.origin.y+keyboardBounds.size.height)
- (self.bounds.origin.y+self.bounds.size.height));

CGRect rectInScreen = [firstResponder convertRect:firstResponder.bounds toView:nil];
if ( rectInScreen.origin.y + rectInScreen.size.height >= [[UIScreen mainScreen] bounds].size.height - keyboardBounds.size.height ) {
CGRect firstResponderFrame = [firstResponder convertRect:firstResponder.bounds toView:self];
if ( firstResponderFrame.origin.y + firstResponderFrame.size.height >= screenBounds.origin.y + screenBounds.size.height - keyboardBounds.size.height ) {
// Prepare to scroll to make sure the view is above the keyboard
offset = [firstResponder convertRect:firstResponder.bounds toView:self].origin.y + self.contentOffset.y;
offset = firstResponderFrame.origin.y + self.contentOffset.y;
if ( self.contentSize.height - offset < newFrame.size.height ) {
// Scroll to the bottom
offset = self.contentSize.height - newFrame.size.height;
Expand All @@ -84,22 +89,17 @@ - (void)keyboardWillShow:(NSNotification*)notification {
[self setContentOffset:CGPointMake(self.contentOffset.x, offset) animated:YES];
}
[UIView commitAnimations];

resizedForKeyboard = YES;
}

- (void)keyboardWillHide:(NSNotification*)notification {
if ( !resizedForKeyboard ) return;

resizedForKeyboard = NO;
if ( CGRectEqualToRect(priorFrame, CGRectZero) ) return;

// Restore dimensions to prior size
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
CGRect frame = self.frame;
frame.size.height += [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
self.frame = frame;
self.frame = priorFrame;
priorFrame = CGRectZero;
[UIView commitAnimations];
}

Expand Down

0 comments on commit 5153814

Please sign in to comment.