Last active
January 11, 2017 07:41
-
-
Save cozzin/2ed2c7de78382a5937a3a43e37689ef3 to your computer and use it in GitHub Desktop.
change view based on keyboard height
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
@property CGFloat currentKeyboardHeight; | |
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnBottomConstraint; |
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)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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
view를 자르는 방식말고, NSLayoutConstraint를 변경하는 방식이 자연스러움