Skip to content

Instantly share code, notes, and snippets.

@scotopic
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scotopic/0182a9e49e783e171a86 to your computer and use it in GitHub Desktop.
Save scotopic/0182a9e49e783e171a86 to your computer and use it in GitHub Desktop.
iOS7 - UIView autorotation and resizing with keyboard
//
// ALXInfoViewViewController.m
// TestRotationApp
#import "ALXInfoViewViewController.h"
@interface ALXInfoViewViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;
@end
@implementation ALXInfoViewViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)viewDidAppear:(BOOL)animated
{
[self.textView becomeFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// --------------------- WORKS 1 -------------------------------------------------
// http://stackoverflow.com/questions/7169702/how-to-resize-uitextview-on-ios-when-a-keyboard-appears
//- (void)moveTextViewForKeyboard:(NSNotification*)aNotification up:(BOOL)up {
//
// NSDictionary* userInfo = [aNotification userInfo];
// NSTimeInterval animationDuration;
// UIViewAnimationCurve animationCurve;
// CGRect keyboardEndFrame;
//
// [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
// [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
// [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
//
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:animationDuration];
// [UIView setAnimationCurve:animationCurve];
//
// CGRect newFrame = self.view.bounds;
// CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
//// keyboardFrame.size.height -= tabBarController.tabBar.frame.size.height;
// newFrame.size.height -= keyboardFrame.size.height * (up?1:-1);
// newFrame = [self.view convertRect:newFrame toView:nil];
//// self.view.bounds = newFrame;
// self.view.frame = newFrame;
//
//
// [UIView commitAnimations];
//}
//
//- (void)keyboardWillShow:(NSNotification*)aNotification
//{
// [self moveTextViewForKeyboard:aNotification up:YES];
//}
//
//- (void)keyboardWillHide:(NSNotification*)aNotification
//{
// [self moveTextViewForKeyboard:aNotification up:NO];
//}
// ---------------------------- WORKS 2 ----------------------------------
// http://stackoverflow.com/questions/16043371/resize-a-view-when-a-keyboard-appears-ios
//- (void)keyboardWillShow:(NSNotification *)note {
// NSDictionary *userInfo = note.userInfo;
// NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
//
// CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];
//
// [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
// CGRect newFrame = self.view.bounds;
//// self.view.frame = [self.view convertRect:CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y) toView:nil];
// newFrame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
// newFrame = [self.view convertRect:newFrame toView:nil];
// self.view.frame = newFrame;
//
// } completion:nil];
//}
//
//- (void)keyboardWillHide:(NSNotification *)note {
// NSDictionary *userInfo = note.userInfo;
// NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
//
// CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];
//
// [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
// CGRect newFrame = self.view.bounds;
//// self.view.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
// newFrame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
// newFrame = [self.view convertRect:newFrame toView:nil];
// self.view.frame = newFrame;
//
// } completion:nil];
//}
@end
//
// ALXViewController.m
// TestRotationApp
//
#import "ALXViewController.h"
#import "ALXInfoViewViewController.h"
@interface ALXViewController ()
@property (nonatomic, strong) ALXInfoViewViewController *infoVC;
@property (nonatomic, strong) UIView *containerView;
@end
@implementation ALXViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillHideNotification object:nil];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)viewDidAppear:(BOOL)animated
{
self.infoVC = [[ALXInfoViewViewController alloc] initWithNibName:@"ALXInfoView" bundle:nil];
[self.infoVC.view setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
[self.infoVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:self.infoVC animated:YES completion:nil];
}
-(IBAction)modalViewDoneButtonTapped:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// ---------------------------------- WORKS 1 -----------------------------------------
- (void)moveTextViewForKeyboard:(NSNotification*)aNotification up:(BOOL)up {
NSDictionary* userInfo = [aNotification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect newFrame = self.infoVC.view.bounds;
CGRect keyboardFrame = [self.infoVC.view convertRect:keyboardEndFrame toView:nil];
// keyboardFrame.size.height -= tabBarController.tabBar.frame.size.height;
newFrame.size.height -= keyboardFrame.size.height * (up?1:-1);
newFrame = [self.infoVC.view convertRect:newFrame toView:nil];
self.infoVC.view.frame = newFrame;
[UIView commitAnimations];
}
- (void)keyboardWillShow:(NSNotification*)aNotification
{
[self moveTextViewForKeyboard:aNotification up:YES];
}
- (void)keyboardWillHide:(NSNotification*)aNotification
{
[self moveTextViewForKeyboard:aNotification up:NO];
}
// ---------------------------------- WORKS 2 -----------------------------------------
- (void)keyboardWillShow:(NSNotification *)note {
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrameEnd = [self.infoVC.view convertRect:keyboardFrameEnd fromView:nil];
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
CGRect newFrame = self.infoVC.view.bounds;
// self.view.frame = [self.view convertRect:CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y) toView:nil];
newFrame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
newFrame = [self.infoVC.view convertRect:newFrame toView:nil];
self.infoVC.view.frame = newFrame;
} completion:nil];
}
- (void)keyboardWillHide:(NSNotification *)note {
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrameEnd = [self.infoVC.view convertRect:keyboardFrameEnd fromView:nil];
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
CGRect newFrame = self.infoVC.view.bounds;
// self.view.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
newFrame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
newFrame = [self.infoVC.view convertRect:newFrame toView:nil];
self.infoVC.view.frame = newFrame;
} completion:nil];
}
@end
Example of how a UITextField can be autorotated and resized properly while the keyboard is up. The key are 2 things:
1) Using bounds instead of frame
2) Converting rect properly to the proper view.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment