看这个action,假设一个程序员自定义view并添加到他们的应用程序的根视图控制器编程的自定义背景图。以前写了这个代码:
UIView *customBackgroundView = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)]; customBackgroundView.backgroundColor = [UIColor redColor]; customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self.view addSubview:customBackgroundView];在iPhone 5之前的机器,上面的代码块会工作得很好。320×480的逻辑点映射到第640×960的iPhone4/4S与默认2.0比例因子。然而,iPhone 5上,仍然会被映射的高度960像素,并会在短
解决这个问题很简单:
UIView *customBackgroundView = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height)]; customBackgroundView.backgroundColor = [UIColor redColor]; customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self.view addSubview:customBackgroundView];
- (void)loadView { CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; UIView *customView = [[UIView alloc] initWithFrame:applicationFrame]; customView.backgroundColor = [UIColor redColor]; customView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; self.view = customView; }ApplicationFrame 属性
UIScreen
框架将返回当前应用程序的窗口的矩形范围,减去的状态栏占用的面积(如果可见)。您也可以得到公正的边界矩形的屏幕
[[UIScreen mainScreen] bounds]。这两个值都将返回逻辑点,而不是像素。
customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
#define IS_IPHONE ( [[[UIDevice currentDevice] model] isEqualToString:@"iPhone"] ) #define IS_IPOD ( [[[UIDevice currentDevice ] model] isEqualToString:@"iPod touch"] ) #define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f #define IS_IPHONE_5 ( IS_IPHONE && IS_HEIGHT_GTE_568 )
if(IS_IPHONE_5) { NSLog(@"Hey, this is an iPhone 5! Well, maybe. . .what year is it?"); } else { NSLog(@"Bummer, this is not an iPhone 5. . ."); }