lwx*_*ted 184 orientation uiinterfaceorientation ios uiscreen ios8
我在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);
Run Code Online (Sandbox Code Playgroud)
以下是iOS 8的结果:
Currently landscape: No, width: 320.00, height: 568.00
Currently landscape: Yes, width: 568.00, height: 320.00
Run Code Online (Sandbox Code Playgroud)
与iOS 7中的结果相比:
Currently landscape: No, width: 320.00, height: 568.00
Currently landscape: Yes, width: 320.00, height: 568.00
Run Code Online (Sandbox Code Playgroud)
是否有任何文件指定此更改?或者它是iOS 8 API中的临时错误?
vhr*_*kov 173
是的,它在iOS8中取决于方向,而不是bug.您可以从WWDC 2014查看会话214以获取更多信息:"在iOS 8中查看控制器进度"
从演示文稿中引用:
UIScreen现在面向接口:
cba*_*tel 59
是的,它在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;
}
Run Code Online (Sandbox Code Playgroud)
Max*_*axK 34
是的,确实,屏幕尺寸现在取决于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;
}
Run Code Online (Sandbox Code Playgroud)
mne*_*mia 32
是的,它现在取决于方向.
我更喜欢下面的方法,以一种与方向无关的方式获得屏幕尺寸以上的一些答案,因为它更简单,因为它不依赖于任何方向代码(其状态可能取决于他们被叫的时间)或版本检查.您可能需要新的iOS 8行为,但如果您需要它在所有iOS版本上保持稳定,这将起作用.
+(CGSize)screenSizeOrientationIndependent {
CGSize screenSize = [UIScreen mainScreen].bounds.size;
return CGSizeMake(MIN(screenSize.width, screenSize.height), MAX(screenSize.width, screenSize.height));
}
Run Code Online (Sandbox Code Playgroud)
Ant*_*ine 11
与此问题相关,因为它解决了我的问题,这里有两个用于屏幕宽度和高度计算的定义:
#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)
Run Code Online (Sandbox Code Playgroud)
如果您同时支持iOS 7和iOS 8,这是解决此问题的最佳解决方案.
你可以使用nativeBounds(与方向无关)
nativeBounds
物理屏幕的边界矩形,以像素为单位.(只读)
声明SWIFT
Run Code Online (Sandbox Code Playgroud)var nativeBounds: CGRect { get }此矩形基于纵向方向的设备.设备旋转时,此值不会更改.
检测设备的高度:
if UIScreen.mainScreen().nativeBounds.height == 960.0 {
}
Run Code Online (Sandbox Code Playgroud)
检测设备的宽度:
if UIScreen.mainScreen().nativeBounds.width == 640.0 {
}
Run Code Online (Sandbox Code Playgroud)
它不是iOS 8 SDK中的错误.他们依赖于边界界面取向.根据您关于该事实的一些参考或文档的问题,我强烈建议您观看View Controller Advancements in iOS 8它是来自WWDC 2014的 214会话.最有趣的部分(根据你的怀疑)是Screen Coordinates从50:45开始.
是的,它在iOS8中取决于方向.
以下是如何通过SDK和OS版本以iOS 8方式读取边界的一致方法.
#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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84454 次 |
| 最近记录: |