IPhone/IPad: How to get screen width programmatically?

前端 未结 7 527
囚心锁ツ
囚心锁ツ 2020-11-29 00:36

Hi I\'m wondering if there\'s a way to get the width programmatically.

I\'m looking for something general enough to accomodate iphone 3gs, iphone 4, ipad. Also, the

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 01:05

    As of iOS 9.0 there's no way to get the orientation reliably. This is the code I used for an app I design for only portrait mode, so if the app is opened in landscape mode it will still be accurate:

    screenHeight = [[UIScreen mainScreen] bounds].size.height;
    screenWidth = [[UIScreen mainScreen] bounds].size.width;
    if (screenWidth > screenHeight) {
        float tempHeight = screenWidth;
        screenWidth = screenHeight;
        screenHeight = tempHeight;
    }
    

提交回复
热议问题