View aligned to top of keyboard appears in wrong place in iOS 9 Shortcut Bar only mode(在 iOS 9 Shortcut Bar only 模式下,与键盘顶部对齐的视图出现在错误的位置)
问题描述
iOS 9 为 iOS 8 添加了
(注意红色视图是如何连接到键盘顶部的)
实际行为:
<块引用>将红色视图附加到键盘顶部的正确方法是什么?
问题在于大多数代码 (
当键盘处于仅快捷栏模式时,这是矩形:
<块引用>请注意键盘的大部分是如何在屏幕外的,但它仍然是相同的高度.
要获得正确的行为,请使用 CGRectIntersection 与视图的边界和该视图内的键盘框架:
//✅ 好代码,使用CGRect keyboardScreenEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];CGRect keyboardViewEndFrame = [self.view convertRect:keyboardScreenEndFrame fromView:self.view.window];CGRect keyboardFrame = CGRectIntersection(self.view.bounds, keyboardViewEndFrame);CGFloat keyboardHeight = keyboardFrame.size.height;//= 55出于同样的原因,应该使用 UIKeyboardFrameEndUserInfoKey 而不是 UIKeyboardFrameBeginUserInfoKey.
iOS 9 adds a Shortcut Bar to the iOS 8 QuickType bar.
As part of this change, if you connect a bluetooth keyboard to an iPad, the keyboard is in a minimized Shortcut Bar only mode (which can be simulated by pressing command-k in the simulator).
I have code which gets the keyboard height using a method similar to the following:
CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardHeight = keyboardFrame.size.height; // = 313
The problem is that when the keyboard is toggled between the expanded and collapsed state, the height remains the same, causing my view to appear in its old location:
Desired behavior:
(Notice how the red view is attached to the top of the keyboard)
Actual behavior:
What's the correct way to get the red view to be attached to the top of the keyboard?
The problem is that most code out there (including Apple's) ignores the fact that UIKeyboardFrameEndUserInfoKey is a CGRect and not a CGSize.
// ❌ Bad code, do not use
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = activeField.superview.frame;
bkgndRect.size.height += kbSize.height;
[activeField.superview setFrame:bkgndRect];
[scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height) animated:YES];
}
Here you see that only the keyboard height (kbSize.height) is being used. The origin of the rect is important, and should not be ignored.
When the keyboard is visible, this is the rect that is reported:
When the keyboard is in Shortcut Bar only mode, this is the rect:
Notice how the majority of the keyboard is offscreen, yet it is still the same height.
To get the correct behavior, use CGRectIntersection with the view's bounds and the keyboard frame within that view:
// ✅ Good code, use
CGRect keyboardScreenEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardViewEndFrame = [self.view convertRect:keyboardScreenEndFrame fromView:self.view.window];
CGRect keyboardFrame = CGRectIntersection(self.view.bounds, keyboardViewEndFrame);
CGFloat keyboardHeight = keyboardFrame.size.height; // = 55
For this same reason, UIKeyboardFrameEndUserInfoKey should be used as opposed to UIKeyboardFrameBeginUserInfoKey.
这篇关于在 iOS 9 Shortcut Bar only 模式下,与键盘顶部对齐的视图出现在错误的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 iOS 9 Shortcut Bar only 模式下,与键盘顶部对齐的
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
