IPhone/IPad: How to get screen width programmatically?

前端 未结 7 552
囚心锁ツ
囚心锁ツ 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:15

    Here is a Swift way to get screen sizes, this also takes current interface orientation into account:

    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
    }
    

    These are included as a standard function in:

    https://github.com/goktugyil/EZSwiftExtensions

提交回复
热议问题