Ken*_*son 210
我使用以下方法确定iOS 7.1中的键盘框架.
在我的视图控制器的init方法中,我注册了UIKeyboardDidShowNotification
:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardOnScreen:) name:UIKeyboardDidShowNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
然后,我使用以下代码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));
}
Run Code Online (Sandbox Code Playgroud)
迅速
与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)")
}
Run Code Online (Sandbox Code Playgroud)
以及计算键盘大小的方法:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardOnScreen:) name:UIKeyboardDidShowNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
Swift 2.0
-(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));
}
Run Code Online (Sandbox Code Playgroud)
以及计算键盘大小的方法:
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)")
}
Run Code Online (Sandbox Code Playgroud)
Swift 3.0
观察
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardOnScreen:) name:UIKeyboardDidShowNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
方法
-(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));
}
Run Code Online (Sandbox Code Playgroud)
Mik*_*ill 46
请记住,对于iOS 8,屏幕键盘的大小可能会有所不同.不要以为屏幕键盘始终可见(具有特定高度)或不可见.
现在,与iOS 8,用户还可以刷卡上下车文本的预测区域...并做到这一点时,他们,这将揭开序幕的应用程序的keyboardWillShow
事件再次.
这将打破许多遗留代码示例,建议编写一个keyboardWillShow
事件,该事件仅测量屏幕键盘的当前高度,并按页面(绝对)量向上或向下移动控件.
换句话说,如果您看到任何示例代码,它只是告诉您添加keyboardWillShow
事件,请测量键盘高度,然后按此数量调整控件的高度,这将不再有效.
在上面的示例中,我使用了以下站点中的示例代码,该代码为垂直约束constant
值设置了动画.
在我的应用程序中,我为我添加了一个约束UITextView
,设置在屏幕的底部.当屏幕首次出现时,我存储了这个初始垂直距离.
然后,每当我的keyboardWillShow
事件开始时,我将(新)键盘高度添加到此原始约束值(因此约束调整控件高度的大小).
是啊.它很丑.
我有点恼火/感到惊讶的是,XCode 6的可怕痛苦的AutoLayout不仅允许我们将控件的底部连接到屏幕的底部或屏幕键盘的顶部.
也许我错过了一些东西.
除了我的理智.
Eri*_*k B 33
纵向模式的键盘高度为216pts,横向模式的键盘高度为162pts.
Phi*_*der 16
版本说明:这不再是iOS 9和10中的值,因为它们支持自定义键盘大小.
这取决于型号和QuickType栏:
http://www.idev101.com/code/User_Interface/sizes.html
小智 10
键盘高度取决于型号,QuickType栏,用户设置......最好的方法是以动态计算:
Swift 3.0
var heightKeyboard : CGFloat?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}
func keyboardShown(notification: NSNotification) {
if let infoKey = notification.userInfo?[UIKeyboardFrameEndUserInfoKey],
let rawFrame = (infoKey as AnyObject).cgRectValue {
let keyboardFrame = view.convert(rawFrame, from: nil)
self.heightKeyboard = keyboardFrame.size.height
// Now is stored in your heightKeyboard variable
}
}
Run Code Online (Sandbox Code Playgroud)
我找不到最新的答案,所以我用模拟器检查一下.(iOS 11.0)
设备| 屏幕高度| 肖像| 景观
iPhone 4s | 480.0 | 216.0 | 162.0
iPhone 5,iPhone 5s,iPhone SE | 568.0 | 216.0 | 162.0
iPhone 6,iPhone 6s,iPhone 7,iPhone 8,iPhone X | 667.0 | 216.0 | 162.0
iPhone 6 plus,iPhone 7 plus,iPhone 8 plus | 736.0 | 226.0 | 162.0
iPad第5代,iPad Air,iPad Air 2,iPad Pro 9.7,iPad Pro 10.5,iPad Pro 12.9 | 1024.0 | 265.0 | 353.0
谢谢!
归档时间: |
|
查看次数: |
108023 次 |
最近记录: |