Last active
May 24, 2021 03:44
-
-
Save choco0908/241e0ecbc17ec2fefc2ce2fbdf6d506d to your computer and use it in GitHub Desktop.
Calculate UIKeyboard 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 keyboardHeight; | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
_keyboardHeight = 0; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(keyboardWillChange:) | |
name:UIKeyboardWillChangeFrameNotification | |
object:nil]; | |
} | |
- (void) keyboardWillChange:(NSNotification *)notification { | |
NSDictionary* info = notification.userInfo; | |
UIWindow *window = [UIApplication sharedApplication].keyWindow; | |
CGRect kbRect = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue]; | |
CGRect kbframe = [window convertRect:kbRect toWindow:window]; | |
CGFloat fromBottom = (CGRectGetHeight([UIScreen mainScreen].bounds)- CGRectGetMinY(kbframe)); | |
keyboardHeight = fromBottom; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment