开发者

How to get screen size using code on iOS?

How to get the iPhone screen s开发者_如何学编程ize to give calculation?


You can use the bounds property on an instance of UIScreen:

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

Most people will want the main screen; if you have a device attached to a TV, you may instead want to iterate over UIScreens and get the bounds for each.

More info at the UIScreen class docs.


Here is a 'swift' solution: (Updated for swift 3.x)

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

This reports in points not pixels and "always reflect[s] the screen dimensions of the device in a portrait-up orientation"(Apple Docs). No need to bother with UIAnnoyinglyLongDeviceOrientation!

If you want the width and height to reflect the device orientation:

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

Here width and height will flip flop values depending on whether the screen is in portrait or landscape.

And here is how to get screen size measured in pixels not points:

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

This also "is based on the device in a portrait-up orientation. This value does not change as the device rotates."(Apple Docs)

Please note that for swift 2.x and lower, you should use UIScreen.mainScreen() instead of UIScreen.main


Use This Code to fix in 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;
}


Use the property bounds of UIView if you want to know the size of a certain view or [[UIScreen mainScreen] bounds] if you want to know the screen size of the device.


Similar to above but slightly less verbose:

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


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);
}


Here is a Swift way to get screen sizes:

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 here.

This is also in the documentation.


[[UIScreen mainScreen] bounds] returns physical screen dimensions not taking device orientation into account.

If you need to get screen size according to the current orientation please look at How to get orientation-dependent height and width of the screen?


Using the UIScreen's bounds is a good way to do it but you should us the the CGGeometry functions to access a CGRect's data rather than accessing it directly. See the CGGeometry docs.

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);


Assuming you want to take the current orientation into account, use:

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;
}


For fellow Xamarians - here is the way you can get the screen size in Xamarin.iOS + C#:

var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;


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;

}

How to use:

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))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜