web-dev-qa-db-fra.com

Comment obtenir la taille de l'écran en utilisant le code sur iOS?

Comment obtenir la taille de l'écran de l'iPhone pour effectuer un calcul?

44
haisergeant

Vous pouvez utiliser la propriété bounds sur une instance de UIScreen:

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

La plupart des gens voudront l'écran principal; si vous avez un périphérique connecté à un téléviseur, vous pouvez plutôt effectuer une itération sur UIScreens et obtenir les limites de chacun.

Plus d'infos sur le Documents de la classe UIScreen .

93
Tim

Voici une solution 'Swift': (Mise à jour pour Swift 3.x)

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

Ceci indique les points et non les pixels et "reflète toujours les dimensions de l'écran du périphérique dans une orientation portrait" (Apple Docs) . Pas besoin de s'embêter avec UIAnnoyinglyLongDeviceOrientation!

Si vous souhaitez que la largeur et la hauteur reflètent l'orientation de l'appareil:

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

Ici, largeur et hauteur inverseront les valeurs selon que l'écran est en portrait ou en paysage.

Et voici comment obtenir la taille de l'écran mesurée en pixels et non en points:

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

Ceci "est également basé sur le périphérique dans une orientation portrait. Cette valeur ne change pas lorsque le périphérique tourne." (Apple Docs)

Veuillez noter que pour Swift 2.x et versions antérieures, vous devez utiliser UIScreen.mainScreen() au lieu de UIScreen.main

16
mogelbuster

Utilisez ce code pour corriger dans 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;
}
8
Guru

Voici un moyen rapide d'obtenir les tailles d'écran:

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
}

Ceux-ci sont inclus en tant que fonction standard ici .

Ceci est également dans la documentation.

1
Esqarrouth

Utilisez la propriété bounds of UIView si vous souhaitez connaître la taille d'une vue ou [[UIScreen mainScreen] bounds] si vous souhaitez connaître la taille d'écran du périphérique.

1
tobiasbayer

Semblable à ci-dessus mais légèrement moins verbeux:

CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
1
simon_smiley
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);
}
1
Bobby

Utiliser les limites de UIScreen est un bon moyen de le faire, mais vous devriez utiliser les fonctions CGGeometry pour accéder aux données de CGRect plutôt que de les accéder directement. Voir le CGGeometrydocs .

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

[[UIScreen mainScreen]] bounds] renvoie les dimensions de l'écran physique ne tenant pas compte de l'orientation du périphérique.

Si vous avez besoin d’obtenir la taille de l’écran en fonction de l’orientation actuelle, veuillez regarder Comment obtenir une hauteur et une largeur dépendant de l’orientation?

0
Mike

En supposant que vous souhaitiez prendre en compte l’orientation actuelle, utilisez:

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