gpt4 book ai didi

cocoa-touch - iOS中如何获取屏幕的宽高?

转载 作者:IT王子 更新时间:2023-10-29 07:24:24 27 4
gpt4 key购买 nike

如何获取 iOS 中屏幕的尺寸?

目前,我使用:

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

viewWillAppear:willAnimateRotationToInterfaceOrientation:duration:

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

最佳答案

How can one get the dimensions of the screen in iOS?

您发布的代码的问题在于您指望 View 大小与屏幕大小相匹配,正如您所见,情况并非总是如此。如果您需要屏幕尺寸,您应该查看代表屏幕本身的对象,如下所示:

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

Split View的更新:在评论中,Dmitry 问道:

How can I get the size of the screen in the split view?

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

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

swift 4.2

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

// split screen
let windowRect = self.view.window?.frame
let windowWidth = windowRect?.size.width
let windowHeight = windowRect?.size.height

关于cocoa-touch - iOS中如何获取屏幕的宽高?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5677716/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com