Skip to content

Instantly share code, notes, and snippets.

@sebastienwindal
Last active August 29, 2015 14:00
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 sebastienwindal/11382183 to your computer and use it in GitHub Desktop.
Save sebastienwindal/11382183 to your computer and use it in GitHub Desktop.
Make a whole view scroll up to make the control appear when keyboard shows up. self.rootScrollView is a scrollview that stretches in the whole screen.
- (void)observeKeyboard
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)stopObservingKeyboard
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
CGFloat verticalShift = 125.0f;
[UIView animateWithDuration:0.3
animations:^{
self.rootScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.rootScrollView.frame),
CGRectGetHeight(self.rootScrollView.frame) + verticalShift);
self.rootScrollView.contentOffset = CGPointMake(0, verticalShift);
}];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3
animations:^{
self.rootScrollView.contentSize = self.rootScrollView.frame.size;
self.rootScrollView.contentOffset = CGPointMake(0, 0);
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self observeKeyboard];
}
-(void) dealloc
{
[self stopObservingKeyboard];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment