Skip to content

Instantly share code, notes, and snippets.

@cozzin
Last active January 11, 2017 07:41
Show Gist options
  • Save cozzin/2ed2c7de78382a5937a3a43e37689ef3 to your computer and use it in GitHub Desktop.
Save cozzin/2ed2c7de78382a5937a3a43e37689ef3 to your computer and use it in GitHub Desktop.
change view based on keyboard height
@property CGFloat currentKeyboardHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnBottomConstraint;
- (void)viewDidAppear:(BOOL)animated{
[self addKeyboardObserver];
}
- (void)viewDidDisappear:(BOOL)animated{
[self removeKeyboardObserver];
}
-(void) addKeyboardObserver{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(void) removeKeyboardObserver{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification*)notification {
NSLog(@"keyboardWillShow");
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGFloat bottomSpace = kbSize.height + SPACE_UNDER_BUTTON;
if(self.btnBottomConstraint.constant == bottomSpace) return;
[self.view layoutIfNeeded];
[UIView animateWithDuration: 0.3 animations: ^{
self.btnBottomConstraint.constant = bottomSpace;
[self.view layoutIfNeeded];
}];
NSLog(@"view height:%f, width:%f", self.view.frame.size.height, self.view.frame.size.width);
}
- (void)keyboardWillHide:(NSNotification*)notification{
NSLog(@"keyboardWillHide");
if(self.btnBottomConstraint.constant == SPACE_UNDER_BUTTON) return;
[self.view layoutIfNeeded];
[UIView animateWithDuration: 0.3 animations: ^{
self.btnBottomConstraint.constant = SPACE_UNDER_BUTTON;
[self.view layoutIfNeeded];
}];
NSLog(@"view height:%f, width:%f", self.view.frame.size.height, self.view.frame.size.width);
}
@cozzin
Copy link
Author

cozzin commented Jan 11, 2017

view를 자르는 방식말고, NSLayoutConstraint를 변경하는 방식이 자연스러움

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment