iOS7、iOS8 UIScreen方法差異
iOS8 在UI的Storyboard設計上引入Size Classes觀念後,對於畫面的橫向、直向的解釋已經不同,UIScreen上面的bonds、applicationFrame所回報的值也因為這樣而有所不同,接下來在iOS7、iOS8分別執行下面的程式並觀察結果看看有什麼不同?
//-----------start----------- NSLog(@"bounds: width = %f, height = %f", [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height); NSLog(@"applicationFrame: width = %f, height = %f",[UIScreen mainScreen].applicationFrame.size.width,[UIScreen mainScreen].applicationFrame.size.height); //------------end------------
iOS7
- 直向
bounds: width = 768.000000, height = 1024.000000 applicationFrame: width = 768.000000, height = 1004.000000
- 橫向
bounds: width = 768.000000, height = 1024.000000 applicationFrame: width = 748.000000, height = 1024.000000
iOS8
- 直向
bounds: width = 768.000000, height = 1024.000000 applicationFrame: width = 768.000000, height = 1004.000000
- 橫向
bounds: width = 1024.000000, height = 768.000000 applicationFrame: width = 1024.000000, height = 748.000000
沒錯,現在的width、height是以你實際操作角度來看width、height,所以在iOS8中你將iPad橫向之後,width從原先直向的768變成1024,其實iOS8的做法個人覺的與實際操作想法比較OK。
橫向、直向 判別
iOS7
iOS7 中提供一個非常容易使用的方法,如果你要知道現在iPad是直向還是橫向直接像下面程式一樣:
//-----------start----------- - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { // 旋轉之前 } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { // 旋轉之後 // 由 fromInterfaceOrientation 值得知目前是哪個方向 /* UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight */ } //------------end------------
iOS8
iOS8 後需要用另一種方式,這方式是使用width來判斷:
//-----------start----------- - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { if (size.width <= size.height) { // 畫面直向 } else { // 畫面橫向 } } //------------end------------