適應iPhone5的尺寸

看這個action,假設一個程序員自定義view並添加到他們的應用程序的根視圖控制器編程的自定義背景圖。以前寫了這個代碼:

[代碼]c#/cpp/oc代碼:

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像素,並會在短

解決這個問題很簡單:

[代碼]c#/cpp/oc代碼:

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方法:

[代碼]c#/cpp/oc代碼:

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的是一個簡單而有效的方法,以確保子視圖對象動態調整,相對於他們的父視圖。在上面的代碼片斷中,我用這個,以確保適當的寬度和高度的自定義背景的視圖對象將擴展方向的變化:

[代碼]c#/cpp/oc代碼:

1 customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

需要注意的是Xcode/ Interface Builder中的autoresizingMask屬性可以控制的。
設備檢測
另一種方法是試圖通過一些檢查,如果當前設備是在運行一個iPhone 5。我發現是最高級的版本。以下是修改後的版本中創建的宏:

[代碼]c#/cpp/oc代碼:

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上運行的代碼。你可以在自己的代碼中使用的最終版本是這樣的:

[代碼]c#/cpp/oc代碼:

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. . ."); 
8 }
發佈了190 篇原創文章 · 獲贊 0 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章