Skip to content

Instantly share code, notes, and snippets.

@choco0908
Last active May 24, 2021 03:44
Show Gist options
  • Save choco0908/241e0ecbc17ec2fefc2ce2fbdf6d506d to your computer and use it in GitHub Desktop.
Save choco0908/241e0ecbc17ec2fefc2ce2fbdf6d506d to your computer and use it in GitHub Desktop.
Calculate UIKeyboard Height
@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