ios - iPhone屏幕键盘的高度是多少?
我使用以下方法来确定 iOS 7.1 中的键盘框架。
在我的视图控制器的 init 方法中,我注册了UIKeyboardDidShowNotification:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardOnScreen:) name:UIKeyboardDidShowNotification object:nil];
然后,我使用下面的代码keyboardOnScreen:来访问键盘的框架。此代码userInfo从通知中获取字典,然后访问NSValue关联的UIKeyboardFrameEndUserInfoKey. 然后,您可以访问 CGRect 并将其转换为视图控制器的视图坐标。从那里,您可以根据该框架执行所需的任何计算。
-(void)keyboardOnScreen:(NSNotification *)notification
{
NSDictionary *info = notification.userInfo;
NSValue *value = info[UIKeyboardFrameEndUserInfoKey];
CGRect rawFrame = [value CGRectValue];
CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];
NSLog(@"keyboardFrame: %@", NSStringFromCGRect(keyboardFrame));
}
迅速
以及 Swift 的等效实现:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
@objc
func keyboardDidShow(notification: Notification) {
guard let info = notification.userInfo else { return }
guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardFrame = frameInfo.cgRectValue
print("keyboardFrame: \(keyboardFrame)")
}