Height and width for landscape mode is showing wrong

二次信任 提交于 2019-12-11 19:24:35

问题


I have used this method for detecting the width and height of screen. But its showing the width as 768 and height as 1024 in portrait and landscape also.

CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

float widthfloat=  screenBounds.size.width;
float heightfloat= screenBounds.size.height;

NSLog(@"  width float %f",widthfloat);
NSLog(@"height float %f",heightfloat);


NSLog(@"width view %f \n height view %f", self.view.bounds.size.width, self.view.bounds.size.height);
NSLog(@"width %f \n height %f", screenBounds.size.width, screenBounds.size.height);


float wvalue  = [[UIScreen mainScreen] bounds].size.width;

float hvalue =  [[UIScreen mainScreen] bounds].size.height;


NSLog(@"  wvalue %f",wvalue);
NSLog(@"hvalue %f",hvalue);


CGFloat width1 = [UIScreen mainScreen].bounds.size.width;
CGFloat height1 = [UIScreen mainScreen].bounds.size.height;


NSLog(@"width1 %f",width1);
NSLog(@"height1 %f",height1);

CGFloat screenScale = [[UIScreen mainScreen] scale];

CGRect screenBounds1 = [[UIScreen mainScreen] bounds];

CGSize screenSize = CGSizeMake(screenBounds1.size.width * screenScale, screenBounds1.size.height * screenScale); if (screenSize.height==1136.000000)

NSLog(@"abcd1 %f",screenBounds1.size.width);
NSLog(@"abcd2 %f",screenBounds1.size.height);
NSLog(@"efgh1 %f",screenSize.width);
NSLog(@"efgh2 %f",screenSize.height);

回答1:


bounds contains the bounding rectangle of the screen, measured in points. It does not care about screen orientation.




回答2:


You can calculate the screen size, I do something like this for me -

-(CGSize)screenSize
{
    CGSize size = [UIScreen mainScreen].applicationFrame.size;

    if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
        return size;

    else
    {
        if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        {
            if (size.height > 480.0)
                // This is iPhone-5
                size = CGSizeMake(568.0, 320.0);
            else
                // This is iPhone-4
                size = CGSizeMake(480.0, 320.0);
        }
        else
        {
            // This is iPad
            size = CGSizeMake(1024.0, 768.0);
        }

        if (![[UIApplication sharedApplication] isStatusBarHidden])
            size.height -= 20.0;

        return size;
    }
}

Note: The method will give you the height after reducing the height of the statusbar if available.



来源:https://stackoverflow.com/questions/18207038/height-and-width-for-landscape-mode-is-showing-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!