我正在尝试在显示键盘时向上滚动文本视图。我正试图在https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html关注Apple自己的教程。我使用autolayout而不是滚动视图和框架,但总体思路是相同的(订阅键盘通知,并相应地设置布局约束)。但是,当我显示键盘时,文本视图和键盘之间存在间隙:(这是来自土耳其语键盘的屏幕截图,但所有安装的键盘中都存在标有红色的相同间隙,包括自定义键盘)。 (iPhone 6 Plus的屏幕截图,但问题也出现在其他屏幕尺寸上)
我已经看过can't get correct value of keyboard height in iOS8而我已尝试同时使用UIKeyboardFrameEndUserInfoKey
和UIKeyboardFrameBeginUserInfoKey
,但它们都会导致相同的差距。我已尝试附加UIKeyboardWillShowNotification
和UIKeyboardDidShowNotification
,但我仍然有同样的差距。我认为差距与iOS 8上某些键盘上的建议有关,但是当建议不存在时,它不应该通过建议报告键盘大小。
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self registerForKeyboardNotifications];
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWillBeShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGRect kb = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
float height = kb.size.height;
sendZoneBottomConstraint.constant = height;
[UIView animateWithDuration:.2 animations:^{
[self.view layoutIfNeeded];
}];
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
sendZoneBottomConstraint.constant = 0;
[UIView animateWithDuration:.2 animations:^{
[self.view layoutIfNeeded];
}];
}
sendZoneBottomConstraint
是底部布局指南底部最底部视图的底部间距。
更新:我已尝试将键盘矩形从窗口坐标更改为我的视图坐标,但它没有改变任何内容。这是我试过的:
CGRect kb = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
kb = [self.view.window convertRect:kb toView:self.view];
float height = kb.size.height;
更新,第2卷:我也见过iOS 8 Orientation change: Keyboard frame does not display correctly,但我在iPhone 5s iOS 7.1模拟器上遇到了这个问题,所以它也是不是iOS 8唯一的问题。 我怎么解决这个问题?我绝对不想在iOS 8世界中硬编码任何键盘高度值。
更新,第3卷:我还尝试使用英文键盘打开和关闭建议。它仍然出现在键盘的总大小上,所以它与建议/自动完成视图无关:
答案 0 :(得分:16)
也许您正在使用标签栏控制器,如果是,那么当您在键盘显示后边缘视图时,计算边距底部没有标签栏高度。
您可以使用以下方法找到标签栏高度:
self.tabBarController?.tabBar.frame.height
答案 1 :(得分:0)
要进行可靠的键盘高度计算,可以使用以下命令:
注意:我为此解决方案从IQKeyboard借用了一些逻辑。
CGRect kbFrame = [[aNotification userInfo][UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect screenSize = [[UIScreen mainScreen] bounds];
CGRect intersectRect = CGRectIntersection(kbFrame, screenSize);//Calculating actual keyboard displayed size, keyboard frame may be different when hardware keyboard is attached (Bug ID: #469) (Bug ID: #381)
CGSize kbSize;
if (CGRectIsNull(intersectRect)) {
kbSize = CGSizeMake(screenSize.size.width, 0);
} else {
kbSize = intersectRect.size;
}
//
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (rootViewController) {
if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController;
UIViewController *nextRootViewController = [[navigationController viewControllers] lastObject];
if (nextRootViewController) {
rootViewController = nextRootViewController;
continue;
} else {
break;
}
}
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabBarController = (UITabBarController *)rootViewController;
UIViewController *nextRootViewController = tabBarController.selectedViewController;
if (nextRootViewController) {
rootViewController = nextRootViewController;
continue;
} else {
break;
}
}
if (!rootViewController.presentedViewController) {
break;
}
rootViewController = (UIViewController *)rootViewController.presentedViewController;
}
//
CGFloat navigationBarAreaHeight = [[UIApplication sharedApplication] statusBarFrame].size.height + rootViewController.navigationController.navigationBar.frame.size.height;
CGFloat layoutAreaHeight = rootViewController.view.layoutMargins.top;
CGFloat topLayoutGuide = MAX(navigationBarAreaHeight, layoutAreaHeight);
CGFloat bottomLayoutGuide = rootViewController.view.layoutMargins.bottom;
float magicNumber = 5;//Apple Constant
//
float keyboardHeight = kbSize.height-topLayoutGuide-bottomLayoutGuide+magicNumber;