Last active
August 29, 2015 14:00
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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