旧工程适应 iphone5 坐标的改变

转自 http://blog.csdn.net/jinglijun/article/details/8095370

看这个action,假设一个程序员自定义view并添加到他们的应用程序的根视图控制器编程的自定义背景图。以前写了这个代码:

[html] view plain copy
  1. UIView *customBackgroundView = [[UIView alloc]  
  2.                                 initWithFrame:  
  3.                                 CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];  
  4. customBackgroundView.backgroundColor = [UIColor redColor];  
  5. customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;  
  6. [self.view addSubview:customBackgroundView];  
在iPhone 5之前的机器,上面的代码块会工作得很好。320×480的逻辑点映射到第640×960的iPhone4/4S与默认2.0比例因子。然而,iPhone 5上,仍然会被映射的高度960像素,并会在短

旧工程适应 iphone5 坐标的改变_第1张图片

解决这个问题很简单:

[html] view plain copy
  1. UIView *customBackgroundView = [[UIView alloc]  
  2.                                 initWithFrame:  
  3.                                 CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height)];  
  4. customBackgroundView.backgroundColor = [UIColor redColor];  
  5. customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;  
  6. [self.view addSubview:customBackgroundView];  

在这种情况下,我们就不得不把目前的根视图的动态大小,以便将新的自定义背景视图,在整个区域。
再举一个例子,让我们假设程序员希望创建一个新的视图以编程方式在loadView方法:
[html] view plain copy
  1. - (void)loadView  
  2. {  
  3.     CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];  
  4.     UIView *customView = [[UIView alloc] initWithFrame:applicationFrame];  
  5.     customView.backgroundColor = [UIColor redColor];  
  6.     customView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;  
  7.     self.view = customView;  
  8. }  
ApplicationFrame 属性   UIScreen  框架将返回当前应用程序的窗口的矩形范围,减去的状态栏占用的面积(如果可见)。您也可以得到公正的边界矩形的屏幕 [[UIScreen mainScreen] bounds]。这两个值都将返回逻辑点,而不是像素。
虽然上面的代码示例是有用的,他们也受到一些限制。在实践中,你可能需要处理更复杂的情况,包括许多子视图动态调整大小根据设备屏幕上的高度。
幸运的是,有至少三种不同的方法,你可以用它来这样做。

View Autoresizing

UIView的属性autoresizingMask的是一个简单而有效的方法,以确保子视图对象动态调整,相对于他们的父视图。在上面的代码片断中,我用这个,以确保适当的宽度和高度的自定义背景的视图对象将扩展方向的变化:
[html] view plain copy
  1. customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;  

需要注意的是Xcode/ Interface Builder中的autoresizingMask属性可以控制的。
设备检测
另一种方法是试图通过一些检查,如果当前设备是在运行一个iPhone 5。我发现是最高级的版本。以下是修改后的版本中创建的宏:
[html] view plain copy
  1. #define IS_IPHONE ( [[[UIDevice currentDevice] model] isEqualToString:@"iPhone"] )  
  2. #define IS_IPOD   ( [[[UIDevice currentDevice ] model] isEqualToString:@"iPod touch"] )  
  3. #define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f  
  4. #define IS_IPHONE_5 ( IS_IPHONE && IS_HEIGHT_GTE_568 )  

第一和第二的宏检查,如果当前设备是一个iPhone或iPod touch上使用UIDevice类​​。
第三宏检查看到,如果屏幕上的高度大于或等于浮点值568。回想一下上面说的[[UIScreen的mainScreen]界消息将返回应用程序窗口的边界框在逻辑点,568,1136像素的逻辑点,将映射到与的默认视图contentScaleFactor的1.0。
最后,第四个宏将两个前宏成IS_IPHONE_5宏(暂时)返回TRUE,如果iPhone5上运行的代码。你可以在自己的代码中使用的最终版本是这样的:

[html] view plain copy
  1. if(IS_IPHONE_5)  
  2. {  
  3.     NSLog(@"Hey, this is an iPhone 5! Well, maybe. . .what year is it?");  
  4. }  
  5. else  
  6. {  
  7.     NSLog(@"Bummer, this is not an iPhone 5. . .");  


你可能感兴趣的:(旧工程适应 iphone5 坐标的改变)