web-dev-qa-db-fra.com

Iphone: obtenir les dimensions de la vue actuelle ou les dimensions de l'écran

Bonjour je veux obtenir la largeur et la hauteur de ma vue principale. Je veux la valeur correcte en mode paysage ou portrait. J'ai essayé ce qui suit:


  NSLog(@"aaa %f", [UIScreen mainScreen].applicationFrame.size.width);
  NSLog(@"zzz %f", self.view.frame.size.width);

Ceux-ci donnent 300 en mode paysage et 320 en mode portrait, oui c'est plus grand en mode portrait. Alors .. ma vue occupe tout l'écran (- barre d'état) donc je m'attends à 480 en mode paysage et 320 en mode portrait. Qu'est-il arrivé au reste des pixels. Dois-je coder en dur ces valeurs? Merci.

33
gyozo kudor

Est-ce que l'une des lignes suivantes fonctionne pour vous?

[[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width

Je suppose que vous pouvez soustraire la hauteur de la barre d'état? Peut-être que je ne comprends pas votre question

100
Omar

Essaye ça :

UIWindow* window = [UIApplication sharedApplication].keyWindow;
NSLog(@"%f",window.frame.size.width);
9
jithinroy

Macros utiles qui vous donneront la taille correcte en fonction de l'orientation de l'appareil:

#define SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
#define SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)
6
sash