问题描述
这只是 iOS 8 的问题,在 iOS 7 中键盘显示正确,设备方向发生了变化.我的应用程序支持纵向和横向,并使用自动布局.
This is only an iOS 8 problem, the keyboard displays correctly in iOS 7 with device orientation change. My application supports both portrait and landscape orientation and uses autolayout.
如果我将包含 UITextField 子类的 UIViewController 子类推送到导航堆栈上,并且 UITextField 成为纵向的第一响应者,则默认键盘会正确显示.但是,如果我将设备旋转到横向,则 UIViewController 子视图布局会正确显示,但键盘会显示在屏幕的顶部中心.键盘的方向对于横向方向是正确的,但它的框架宽度与纵向方向的预期宽度相同.如果我将应用程序方向设置为仅横向,则键盘无法正确显示.这只是 iOS 8 方向更改的问题.
If I push a UIViewController subclass that contains a UITextField subclass onto a navigation stack and the UITextField becomes the first responder in portrait orientation then the default keyboard displays correctly. However, if I rotate the device to landscape orientation then the UIViewController subview layouts are displayed correctly but the keyboard is displayed in the top center of the screen. The keyboard's orientation is correct for landscape orientation but it's frame width is the same width as expected for portrait orientation. If I set the app orientation to only landscape orientation then the keyboard does not display correctly. This is only a problem for iOS 8 orientation change.
推荐答案
在 iOS 8 之前,键盘的位置和宽度/高度在报告给应用程序时总是相对于纵向.(例如,横向的键盘宽度在 y 方向,在 iPad 上约为 352 像素.)从 iOS 8 开始,它已更新为始终在(物理)视图的左上角显示 (0,0),并且宽度/高度反映了您通常在 iOS 之外所期望的 x/y 方向.如果您之前通过 keyboardDidShow 的 [notification userInfo] 之类的方式定位键盘,您将获得不太有意义的数字.您可以使用这些方法来考虑 iOS8 之前的特性:
Prior to iOS 8, the keyboard's location and width/height were always relative to portrait orientation when reported to the app. (e.g. Landscape's keyboard width is in the y direction, ~352 pixels on an iPad.)
As of iOS 8, this has been updated to always have (0,0) at the top left of your (physical) view and the width/height reflect the x/y orientation you would normally expect outside of iOS. If you were previously positioning your keyboard via something like keyboardDidShow's [notification userInfo], you are going to get numbers that don't quite make sense. You can use something along these lines to take into account the pre-iOS8 idiosyncrasies:
- (void)keyboardDidShow: (NSNotification *) notification{
NSDictionary *keyboardInfo = [notification userInfo];
CGSize keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
float height, width;
if(UIInterfaceOrientationIsPortrait(orientation)){
width = keyboardSize.width;
height = keyboardSize.height;
} else {
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1){
width = keyboardSize.height;
height = keyboardSize.width;
} else {
width = keyboardSize.width;
height = keyboardSize.height;
}
}
// Remainder of function
}
可以重构为...
- (void)keyboardDidShow: (NSNotification *) notification{
NSDictionary *keyboardInfo = [notification userInfo];
CGSize keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
float width = keyboardSize.width;
float height = keyboardSize.height;
if(!UIInterfaceOrientationIsPortrait(orientation) && (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1)){
width = keyboardSize.height;
height = keyboardSize.width;
}
// Remainder of function
}
此外,8.1 更新修复了几个可能与上述更改相关的横向/旋转错误.获取更新,看看是否能解决您的问题.
Also, the 8.1 update fixed several landscape/rotation bugs likely related to the above change. Grab the update and see if that solves your issue.
这篇关于iOS 8 方向更改:键盘框架无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)