49

計算するためにiPhoneの画面サイズを取得するにはどうすればよいですか?

4

12 に答える 12

104

boundsUIScreenのインスタンスでプロパティを使用できます。

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;  
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;

ほとんどの人はメイン画面が欲しいでしょう。テレビにデバイスが接続されている場合は、代わりにUIScreenを繰り返し処理して、それぞれの境界を取得することをお勧めします。

詳細については、UIScreenクラスのドキュメントをご覧ください。

于 2010-09-03T12:07:51.037 に答える
26

これが「swift」ソリューションです:(swift3.x用に更新)

let screenWidth  = UIScreen.main.fixedCoordinateSpace.bounds.width
let screenHeight = UIScreen.main.fixedCoordinateSpace.bounds.height

これは、ピクセルではなくポイントでレポートし、「デバイスの画面の寸法を常に縦向きに反映します」(Apple Docs)。UIAnnoyinglyLongDeviceOrientationを気にする必要はありません!

幅と高さをデバイスの向きに反映させたい場合:

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

ここで、幅と高さは、画面が縦向きか横向きかに応じてフリップフロップの値を反転します。

そして、これがポイントではなくピクセルで測定された画面サイズを取得する方法です:

let screenWidthInPixels = UIScreen.main.nativeBounds.width
let screenHeightInPixels = UIScreen.main.nativeBounds.height

これも「縦向きのデバイスに基づいています。この値は、デバイスが回転しても変化しません」。(Apple Docs)

UIScreen.mainScreen()swift 2.x以下の場合は、代わりにを使用する必要があることに注意してくださいUIScreen.main

于 2015-03-01T06:07:53.523 に答える
8

このコードを使用してiOS8で修正します:

float SW;
float SH;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (( [[[UIDevice currentDevice] systemVersion] floatValue]<8)  && UIInterfaceOrientationIsLandscape(orientation))
{
    SW = [[UIScreen mainScreen] bounds].size.height;
    SH = [[UIScreen mainScreen] bounds].size.width;
}
else
{
    SW = [[UIScreen mainScreen] bounds].size.width;
    SH = [[UIScreen mainScreen] bounds].size.height;
}
于 2014-03-24T03:56:54.580 に答える
1

特定のビューのサイズを知りたい場合、またはデバイスの画面サイズを知りたい場合は、のプロパティboundsを使用します。UIView[[UIScreen mainScreen] bounds]

于 2010-09-03T12:07:52.237 に答える
1

上記と同様ですが、少し冗長ではありません。

CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
于 2014-03-24T03:53:33.693 に答える
1
float screen_Height;
float screen_Width;

if ([[[UIDevice currentDevice] systemVersion] floatValue]  >= 8) {
    screen_Height = [[UIScreen mainScreen] bounds].size.height;
    screen_Width = [[UIScreen mainScreen] bounds].size.width;
} else {
    screen_Height = ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width);
    screen_Width = ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height);
}
于 2014-09-17T21:36:37.570 に答える
1

画面サイズを取得するSwiftの方法は次のとおりです。

var screenWidth: CGFloat {
    if UIInterfaceOrientationIsPortrait(screenOrientation) {
        return UIScreen.mainScreen().bounds.size.width
    } else {
        return UIScreen.mainScreen().bounds.size.height
    }
}
var screenHeight: CGFloat {
    if UIInterfaceOrientationIsPortrait(screenOrientation) {
        return UIScreen.mainScreen().bounds.size.height
    } else {
        return UIScreen.mainScreen().bounds.size.width
    }
}
var screenOrientation: UIInterfaceOrientation {
    return UIApplication.sharedApplication().statusBarOrientation
}

これらは標準機能としてここに含まれています。

これはドキュメントにもあります。

于 2015-11-26T07:12:29.380 に答える
0

[[UIScreen mainScreen] bounds]は、デバイスの向きを考慮せずに物理的な画面サイズを返します。

現在の向きに応じて画面サイズを取得する必要がある場合は、画面の向きに依存する高さと幅を取得する方法をご覧ください。

于 2012-08-21T10:10:36.320 に答える
0

'の境界を使用するUIScreenことはそれを行うための良い方法ですが、データに直接アクセスするのではなく、'のデータCGGeometryにアクセスするための関数を使用する必要があります。ドキュメントCGRectを参照してください。CGGeometry

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);
于 2014-07-19T21:49:55.667 に答える
0

現在の向きを考慮に入れたいと仮定して、以下を使用します。

float screen_width;
float screen_height;
float vers = [[[UIDevice currentDevice] systemVersion] floatValue];
UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation;
if (vers < 8 && UIInterfaceOrientationIsLandscape(orient))
{
    screen_height = [[UIScreen mainScreen] bounds].size.width;
    screen_width = [[UIScreen mainScreen] bounds].size.height;
}
else
{
    screen_width = [[UIScreen mainScreen] bounds].size.width;
    screen_height = [[UIScreen mainScreen] bounds].size.height;
}
于 2014-12-02T19:10:52.580 に答える
0

仲間のXamarianの場合-Xamarin.iOS+C#で画面サイズを取得する方法は次のとおりです。

var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;
于 2019-03-26T10:55:41.070 に答える
0
var PT: CGFloat = 0;

override func viewDidLoad() {

    super.viewDidLoad()
    setPoints();

}

func setPoints() {

    let screenBounds = UIScreen.main.bounds

    let screenScale = UIScreen.main.scale

    let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)

    
    let nativeBounds = UIScreen.main.nativeBounds

    let nativeScale = UIScreen.main.nativeScale

    
    let pxScreenWidth:CGFloat = nativeBounds.width;

    let ptScreenWidth:CGFloat = screenBounds.width;

    PT = pxScreenWidth/ptScreenWidth/nativeScale;

}

使い方:

let label: UILabel = UILabel(frame: CGRect(x: 15 * PT, y: 10 * PT, width: self.view.frame.width - 30 * PT, height: self.view.frame.height - 20 * PT))
于 2020-10-01T17:09:30.017 に答える