cocoa touch - swift获取屏幕宽度 - 如何在iOS中获取屏幕宽度和高度?




ios获取屏幕尺寸 (11)

如何获得iOS屏幕的尺寸?

您发布的代码存在的问题是您需要依据视图大小来匹配屏幕大小,正如您所看到的,情况并非总是如此。 如果您需要屏幕尺寸,您应该查看代表屏幕本身的对象,如下所示:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

分裂视图更新:在评论中,德米特里问道:

如何在分割视图中获取屏幕的大小?

上面给出的代码报告了屏幕的大小,即使在分屏模式下也是如此。 当你使用分屏模式时,你的应用程序的窗口会改变。 如果上面的代码不能提供您期望的信息,那么就像OP一样,您正在查看错误的对象。 不过,在这种情况下,您应该看看窗口而不是屏幕,如下所示:

CGRect windowRect = self.view.window.frame;
CGFloat windowWidth = windowRect.size.width;
CGFloat windowHeight = windowRect.size.height;

如何获得iOS屏幕的尺寸?

目前,我使用:

lCurrentWidth = self.view.frame.size.width;
lCurrentHeight = self.view.frame.size.height;

viewWillAppear: and willAnimateRotationToInterfaceOrientation:duration:

我第一次获得整个屏幕尺寸。 第二次我得到屏幕减去导航栏。


CGFloat width = [[UIScreen mainScreen] bounds].size.width; CGFloat height = [[UIScreen mainScreen]bounds ].size.height;


在这里,我已经更新了快速3

应用程序框架已从iOS 9中弃用

在swift中,他们删除了(),并且他们改变了很少的命名约定,您可以在这里参考Link

func windowHeight() -> CGFloat {
    return UIScreen.main.bounds.size.height
}

func windowWidth() -> CGFloat {
    return UIScreen.main.bounds.size.width
}

如果您希望屏幕宽度/高度与设备方向无关(适用于从纵向方向启动纵向仅视图控制器的大小):

CGFloat screenWidthInPoints = [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale;
CGFloat screenHeightInPoints = [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale;

[UIScreen mainScreen] .nativeBounds < - 来自文档 - >物理屏幕的边界矩形,以像素为单位。 此矩形基于纵向设备中的设备。 该值不随设备旋转而改变。


您可以将这些宏放置在您的pch文件中并在项目中的任何位置使用“SCREEN_WIDTH”

#define SCREEN_WIDTH                ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)

和“SCREEN_HEIGHT”

#define SCREEN_HEIGHT               ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation ==   UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)

使用示例:

CGSize calCulateSizze ;
calCulateSizze.width = SCREEN_WIDTH/2-8;
calCulateSizze.height = SCREEN_WIDTH/2-8;

我以前使用过这些便利方法:

- (CGRect)getScreenFrameForCurrentOrientation {
    return [self getScreenFrameForOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

- (CGRect)getScreenFrameForOrientation:(UIInterfaceOrientation)orientation {

    CGRect fullScreenRect = [[UIScreen mainScreen] bounds];

    // implicitly in Portrait orientation.
    if (UIInterfaceOrientationIsLandscape(orientation)) {
      CGRect temp = CGRectZero;
      temp.size.width = fullScreenRect.size.height;
      temp.size.height = fullScreenRect.size.width;
      fullScreenRect = temp;
    }

    if (![[UIApplication sharedApplication] statusBarHidden]) {
      CGFloat statusBarHeight = 20; // Needs a better solution, FYI statusBarFrame reports wrong in some cases..
      fullScreenRect.size.height -= statusBarHeight;
    }

    return fullScreenRect;
} 

我已经将上面的Objective-C的一些答案翻译成了Swift代码。 每个翻译都会参考原始答案。

share

let screen = UIScreen.main.bounds
let screenWidth = screen.size.width
let screenHeight = screen.size.height

share

func windowHeight() -> CGFloat {
    return UIScreen.mainScreen().applicationFrame.size.height
}

func windowWidth() -> CGFloat {
    return UIScreen.mainScreen().applicationFrame.size.width
}

设备定位答案

var screenHeight : CGFloat
let statusBarOrientation = UIApplication.sharedApplication().statusBarOrientation
// it is important to do this after presentModalViewController:animated:
if (statusBarOrientation != UIInterfaceOrientation.Portrait
    && statusBarOrientation != UIInterfaceOrientation.PortraitUpsideDown){
    screenHeight = UIScreen.mainScreen().applicationFrame.size.width
} else {
    screenHeight = UIScreen.mainScreen().applicationFrame.size.height
}

记录答案

let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
println("width: \(screenWidth)")
println("height: \(screenHeight)")

我意识到这是一个旧帖子,但有时我发现#define这些常量是有用的,所以我不必担心它:

#define DEVICE_SIZE [[[[UIApplication sharedApplication] keyWindow] rootViewController].view convertRect:[[UIScreen mainScreen] bounds] fromView:nil].size

无论设备方向如何,上述常数都应该返回正确的大小。 然后获取尺寸如下所示:

lCurrentWidth = DEVICE_SIZE.width;
lCurrentHeight = DEVICE_SIZE.height;

获取设备大小以及考虑方向非常非常容易:

// grab the window frame and adjust it for orientation
UIView *rootView = [[[UIApplication sharedApplication] keyWindow] 
                                   rootViewController].view;
CGRect originalFrame = [[UIScreen mainScreen] bounds];
CGRect adjustedFrame = [rootView convertRect:originalFrame fromView:nil];

迅速3.0

宽度

UIScreen.main.bounds.size.width

为高度

UIScreen.main.bounds.size.height

NSLog(@"%.0f", [[UIScreen mainScreen] bounds].size.width);
NSLog(@"%.0f", [[UIScreen mainScreen] bounds].size.height);






screen