自动调整iOS设备分辨率

时间:2012-02-16 20:00:43

标签: iphone ios ipad

到目前为止,我一直在使用if和else语句来定义iPhone或iPad上显示的对象的大小,例如:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown){
        //For Portrait on iPhone
        [self.navBar setFrame:CGRectMake(0.0 , 0.0, 320.0f, 44.0)];

    } else if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
        //For Landscape on iPhone
        [self.navBar setFrame:CGRectMake(0.0 , 0.0, 480.0f, 44.0)];

    }    
} else {
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown){
        //For Portrait on iPad
        [self.navBar setFrame:CGRectMake(0.0 , 0.0, 768.0f, 44.0)];

    } else if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
        //For Landscape on iPad
        [self.navBar setFrame:CGRectMake(0.0 , 0.0, 1024.0f, 44.0)];

    }      
}

但现在我想改变它以使它更有活力。我试图使用以下但没有运气。

CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);

有人可以帮我解决如何将这两者结合起来的问题吗?似乎我混淆它的方式是问题。如果有人能给我一个模板甚至是暗示,我会非常感激。

非常感谢

1 个答案:

答案 0 :(得分:4)

好像你在想这个。为什么不呢:

[self.navBar setFrame:CGRectMake(0.0 , 0.0, self.view.frame.size.width, 44.0)];