Iphone: Get current view dimensions or screen dimensions

后端 未结 3 757
天命终不由人
天命终不由人 2020-12-28 13:29

Hello i want to get the width and height of my main view. I want the correct value in landscape or portrait mode. I\'ve tried the following:


  NSLog(@\"aaa          


        
相关标签:
3条回答
  • 2020-12-28 13:38

    Useful macros that will give you the correct size based on the device orientation:

    #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)
    
    0 讨论(0)
  • 2020-12-28 13:44

    Try this :

    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    NSLog(@"%f",window.frame.size.width);
    
    0 讨论(0)
  • 2020-12-28 13:51

    Do either of the following lines work for you?

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

    I suppose you can subtract the height of the status bar? Maybe i'm not understanding your question

    0 讨论(0)
提交回复
热议问题