her*_*ube 6 iphone objective-c ios iphone-6-plus
在我的通用应用程序中,我当前supportedInterfaceOrientations在窗口的根视图控制器中覆盖以定义允许的方向.到目前为止,决定是基于设备的用户界面习语:
- (NSUInteger) supportedInterfaceOrientations
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
else
return UIInterfaceOrientationMaskAll;
}
Run Code Online (Sandbox Code Playgroud)
现在我想改变它,以便我也可以支持iPhone 6 Plus的风景,但不支持其他iPhone.我可以想象一两个解决方案,但这些都非常脆弱,并且可能会在Apple开始制造新设备时破坏.
在理想的世界中,我想将上述方法更改为以下代码段,其中决策基于设备的用户界面大小类而不是用户界面惯用语:
- (NSUInteger) supportedInterfaceOrientations
{
// Note the hypothetical UIDevice method "landscapeSizeClass"
if ([[UIDevice currentDevice] landscapeSizeClass] == UIUserInterfaceSizeClassCompact)
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
else
return UIInterfaceOrientationMaskAll;
}
Run Code Online (Sandbox Code Playgroud)
landscapeSizeClass在UIKit的某个地方有类似神奇的方法吗?我在各种课程参考和指南中看了一下,但没有找到任何有用的东西.或者有人可以建议一个同样通用且面向未来的不同解决方案吗?
请注意,我的应用程序以编程方式创建其UI,因此纯粹基于故事板的解决方案已经完成.此外,我的应用程序仍然需要支持iOS 7,所以我不能只是改变一切使用大小类.但是,我可以做的是在使用简单的iOS 8 API之前进行运行时检查.
由于缺乏官方的 Apple API,这是我想出的解决方法:
- (NSUInteger) supportedInterfaceOrientations
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
// iPhone 5S and below: 320x480
// iPhone 6: 375x667
// iPhone 6 Plus: 414x736
CGSize screenSize = [UIScreen mainScreen].bounds.size;
// The way how UIScreen reports its bounds has changed in iOS 8.
// Using MIN() and MAX() makes this code work for all iOS versions.
CGFloat smallerDimension = MIN(screenSize.width, screenSize.height);
CGFloat largerDimension = MAX(screenSize.width, screenSize.height);
if (smallerDimension >= 400 && largerDimension >= 700)
return UIInterfaceOrientationMaskAll;
else
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
else
{
// Don't need to examine screen dimensions on iPad
return UIInterfaceOrientationMaskAll;
}
}
Run Code Online (Sandbox Code Playgroud)
该代码片段只是假设尺寸高于半任意选择的尺寸的屏幕适合旋转。半任意,因为 400x700 的阈值包括 iPhone 6 Plus,但不包括 iPhone 6。
虽然这个解决方案相当简单,但我喜欢它正是因为它不复杂。我真的不需要准确区分设备,因此任何聪明的解决方案(例如Jef 的答案中的解决方案)对于我的目的来说都是矫枉过正的。
| 归档时间: |
|
| 查看次数: |
789 次 |
| 最近记录: |