我在iOS 7和iOS 8中都运行了以下代码:
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
BOOL landscape = (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight);
NSLog(@"Currently landscape: %@, width: %.2f, height: %.2f",
(landscape ? @"Yes" : @"No"),
[[UIScreen mainScreen] bounds].size.width,
[[UIScreen mainScreen] bounds].size.height);
以下是 iOS 8 的结果:
Currently landscape: No, width: 320.00, height: 568.00
Currently landscape: Yes, width: 568.00, height: 320.00
与 iOS 7 中的结果相比:
Currently landscape: No, width: 320.00, height: 568.00
Currently landscape: Yes, width: 320.00, height: 568.00
是否有任何文档指定此更改?还是iOS 8 API中的临时错误?
是的,它在iOS8中依赖于方向,而不是错误。您可以查看 WWDC 2014 中的会话 214 以获取更多信息:"iOS 8 中的视图控制器改进">
演讲引述:
UIScreen现在是面向接口的:
- [UIScreen 边界] 现在面向接口
- [UIScreen applicationFrame] 现在面向接口
- 状态栏帧通知面向接口
- 键盘帧通知面向接口
是的,它在 iOS8 中依赖于方向。
我编写了一个 Util 方法来为需要支持旧版本操作系统的应用程序解决此问题。
+ (CGSize)screenSize {
CGSize screenSize = [UIScreen mainScreen].bounds.size;
if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
return CGSizeMake(screenSize.height, screenSize.width);
}
return screenSize;
}
是的,确实,屏幕尺寸现在在iOS 8中取决于方向。但是,有时需要将尺寸固定为纵向。这是我的做法。
+ (CGRect)screenBoundsFixedToPortraitOrientation {
UIScreen *screen = [UIScreen mainScreen];
if ([screen respondsToSelector:@selector(fixedCoordinateSpace)]) {
return [screen.coordinateSpace convertRect:screen.bounds toCoordinateSpace:screen.fixedCoordinateSpace];
}
return screen.bounds;
}
是的,它现在取决于方向。
我更喜欢以下以与方向无关的方式获取屏幕尺寸的方法,而不是上面的一些答案,因为它更简单,而且因为它不依赖于任何方向代码(其状态可能取决于调用它们的时间(或版本检查。您可能需要新的iOS 8行为,但如果您需要它在所有版本的iOS上都稳定,这将起作用。
+(CGSize)screenSizeOrientationIndependent {
CGSize screenSize = [UIScreen mainScreen].bounds.size;
return CGSizeMake(MIN(screenSize.width, screenSize.height), MAX(screenSize.width, screenSize.height));
}
与这个问题有关,因为它解决了我的问题,这里有两个定义我用于屏幕宽度和高度计算:
#define SCREEN_WIDTH (IOS_VERSION_LOWER_THAN_8 ? (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height) : [[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT (IOS_VERSION_LOWER_THAN_8 ? (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width) : [[UIScreen mainScreen] bounds].size.height)
#define IOS_VERSION_LOWER_THAN_8 (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)
如果您同时支持iOS 7和iOS 8,这是解决此问题的最佳解决方案。
您可以使用nativeBounds
(与方向无关(
nativeBounds
物理屏幕的边框,以像素为单位。 (只读(
声明 环球银行间金融电信协会
var nativeBounds: CGRect { get }
此矩形基于纵向方向的设备。这 值不会随着设备的旋转而改变。
检测设备的高度:
if UIScreen.mainScreen().nativeBounds.height == 960.0 {
}
检测设备的宽度:
if UIScreen.mainScreen().nativeBounds.width == 640.0 {
}
这不是iOS 8 SDK中的错误。他们使边界接口方向依赖于。根据您关于该事实的一些参考或文档的问题,我强烈建议您观看WWDC 2014的214 View Controller Advancements in iOS 8
会议。最有趣的部分(根据您的疑问(是 Screen Coordinates
从 50:45 开始。
是的,它在 iOS8 中依赖于方向。
下面介绍如何以 iOS 8 的方式跨 SDK 和操作系统版本以简洁的方式读取边界。
#ifndef NSFoundationVersionNumber_iOS_7_1
# define NSFoundationVersionNumber_iOS_7_1 1047.25
#endif
@implementation UIScreen (Legacy)
// iOS 8 way of returning bounds for all SDK's and OS-versions
- (CGRect)boundsRotatedWithStatusBar
{
static BOOL isNotRotatedBySystem;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
BOOL OSIsBelowIOS8 = [[[UIDevice currentDevice] systemVersion] floatValue] < 8.0;
BOOL SDKIsBelowIOS8 = floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1;
isNotRotatedBySystem = OSIsBelowIOS8 || SDKIsBelowIOS8;
});
BOOL needsToRotate = isNotRotatedBySystem && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
if(needsToRotate)
{
CGRect screenBounds = [self bounds];
CGRect bounds = screenBounds;
bounds.size.width = screenBounds.size.height;
bounds.size.height = screenBounds.size.width;
return bounds;
}
else
{
return [self bounds];
}
}
@end
我需要一个快速的辅助函数,它与 iOS7 下的 iOS8 保持相同的行为 - 这允许我交换我的[[UIScreen mainScreen] bounds]
调用而不接触其他代码......
+ (CGRect)iOS7StyleScreenBounds {
CGRect bounds = [UIScreen mainScreen].bounds;
if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
bounds.size = CGSizeMake(bounds.size.height, bounds.size.width);
}
return bounds;
}
我的解决方案是MaxK和hfossli的组合。 我在UIScreen类别上制作了此方法,它没有版本检查(这是一种不好的做法(:
//Always return the iOS8 way - i.e. height is the real orientation dependent height
+ (CGRect)screenBoundsOrientationDependent {
UIScreen *screen = [UIScreen mainScreen];
CGRect screenRect;
if (![screen respondsToSelector:@selector(fixedCoordinateSpace)] && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
screenRect = CGRectMake(screen.bounds.origin.x, screen.bounds.origin.y, screen.bounds.size.height, screen.bounds.size.width);
} else {
screenRect = screen.bounds;
}
return screenRect;
}
下面的方法可用于查找给定方向的屏幕边界,与iOS版本无关。此方法将根据设备的屏幕大小返回边界,并将提供独立于iOS版本的相同CGRect值。
- (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation {
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
CGRect bounds = CGRectZero;
if (UIInterfaceOrientationIsLandscape(orientation)) {
bounds.size = CGSizeMake(MAX(width, height), MIN(width, height));
} else {
bounds.size = CGSizeMake(MIN(width, height), MAX(width, height));
}
return bounds;
}
// For the below example, bounds will have the same value if you run the code on iOS 8.x or below versions.
CGRect bounds = [self boundsForOrientation:UIInterfaceOrientationPortrait];
iOS 8 或更高版本
对于那些想要以点为单位找出屏幕尺寸的人(3.5英寸屏幕有320×480点,4.0英寸屏幕有320×568点等(的解决方案将是
- (CGSize)screenSizeInPoints
{
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
if (width > height) {
return CGSizeMake(height, width);
}
else {
return [[UIScreen mainScreen] bounds].size;
}
}
这就是我用来计算正确矩形的方法:
UIScreen* const mainScreen = [UIScreen mainScreen];
CGRect rect = [mainScreen bounds];
#ifdef __IPHONE_8_0
if ([mainScreen respondsToSelector:@selector(coordinateSpace)])
{
if ([mainScreen respondsToSelector:@selector(fixedCoordinateSpace)])
{
id tmpCoordSpace = [mainScreen coordinateSpace];
id tmpFixedCoordSpace = [mainScreen fixedCoordinateSpace];
if ([tmpCoordSpace respondsToSelector:@selector(convertRect:toCoordinateSpace:)])
{
rect = [tmpCoordSpace convertRect:rect toCoordinateSpace: tmpFixedCoordSpace];
}
}
}
#endif
只需添加上面回答的优秀 cbartel 函数的快速版本。
func screenSize() -> CGSize {
let screenSize = UIScreen.mainScreen().bounds.size
if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) {
return CGSizeMake(screenSize.height, screenSize.width)
}
return screenSize
}
我的问题与 UIWindows 框架有关,该帧正在负数。所以在MyViewController中制作了如下代码-(NSUInteger(支持的接口方向方法
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.view setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
[appDel.window setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
它对我的工作可以尝试一下。
使用了略微修改的 mnemia 解决方案,即没有 iOS 版本检查的解决方案,在主屏幕边界上使用最小/最大值。
我需要一CGRect
所以从主屏幕边界CGRect
并更改了size.width=min(w,h)
,size.height=max(w,h)
。然后我在代码中的两个地方调用了独立于操作系统的获取CGRect
函数,在那里我获取了OpenGL
、触摸等的屏幕尺寸。在修复之前,我有 2 个问题 - 在横向模式下的 IOS 8.x 上OpenGL
视图的显示位置不正确:左下角全屏的 1/4。第二次触摸返回无效值。如前所述,这两个问题都已修复。谢谢!
我注意到的一件事是,Info.plist 中支持的界面方向的顺序确实很重要。我的应用程序遇到了这个问题的问题(在代码中做方向(,但我没有在任何地方指定默认方向是 Portrait。
我认为默认方向无论如何都是肖像。
在 Info.plist 中重新排列 itens,将 Portrait 放在首位,恢复了预期的行为。
这将在 iOS7 和 iOS8 中提供正确的设备,
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define IS_PORTRAIT UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)
+ (BOOL)isIPHONE4{
// < iOS 8.0
if(SYSTEM_VERSION_LESS_THAN(@"8.0")){
if ([self getDeviceWidth] == 320.0 && [self getDeviceHeight] == 480.0) {
return YES;
} else {
return NO;
}
// >= iOS 8.0
}else{
if(IS_PORTRAIT){
if ([self getDeviceWidth] == 320.0 && [self getDeviceHeight] == 480.0) {
return YES;
} else {
return NO;
}
}else{
if ([self getDeviceWidth] == 480.0 && [self getDeviceHeight] == 320.0) {
return YES;
} else {
return NO;
}
}
}
}
+ (BOOL)isIPHONE5{
// < iOS 8.0
if(SYSTEM_VERSION_LESS_THAN(@"8.0")){
if ([self getDeviceWidth] == 320.0 && [self getDeviceHeight] == 568.0) {
return YES;
} else {
return NO;
}
// >= iOS 8.0
}else{
if(IS_PORTRAIT){
if ([self getDeviceWidth] == 320.0 && [self getDeviceHeight] == 568.0) {
return YES;
} else {
return NO;
}
}else{
if ([self getDeviceWidth] == 568.0 && [self getDeviceHeight] == 320.0) {
return YES;
} else {
return NO;
}
}
}
}
+ (BOOL)isIPHONE6{
// < iOS 8.0
if(SYSTEM_VERSION_LESS_THAN(@"8.0")){
if ([self getDeviceWidth] == 375.0 && [self getDeviceHeight] == 667.0) {
return YES;
} else {
return NO;
}
// >= iOS 8.0
}else{
if(IS_PORTRAIT){
if ([self getDeviceWidth] == 375.0 && [self getDeviceHeight] == 667.0) {
return YES;
} else {
return NO;
}
}else{
if ([self getDeviceWidth] == 667.0 && [self getDeviceHeight] == 375.0) {
return YES;
} else {
return NO;
}
}
}
}
+ (BOOL)isIPHONE6Plus{
// < iOS 8.0
if(SYSTEM_VERSION_LESS_THAN(@"8.0")){
if ([self getDeviceWidth] == 414.0 && [self getDeviceHeight] == 736.0) {
return YES;
} else {
return NO;
}
// >= iOS 8.0
}else{
if(IS_PORTRAIT){
if ([self getDeviceWidth] == 414.0 && [self getDeviceHeight] == 736.0) {
return YES;
} else {
return NO;
}
}else{
if ([self getDeviceWidth] == 736.0 && [self getDeviceHeight] == 414.0) {
return YES;
} else {
return NO;
}
}
}
}
+ (CGFloat)getDeviceHeight{
//NSLog(@"Device width: %f",[UIScreen mainScreen].bounds.size.height);
return [UIScreen mainScreen].bounds.size.height;
}
+ (CGFloat)getDeviceWidth{
//NSLog(@"Device width: %f",[UIScreen mainScreen].bounds.size.height);
return [UIScreen mainScreen].bounds.size.width;
}
您也可以添加更多设备(即iPad(。