iOS6 旋轉

iOS 6的rotation改變了不少。先來看看官方的描述  http://www.bgr.com/2012/08/06/ios-6-beta-4-change-log-now-available/ios


知識點:app

*UIViewController的shouldAutorotateToInterfaceOrientation方法被deprecated。在ios6裏,是使用supportedInterfaceOrientations and shouldAutorotate 2個方法來代替shouldAutorotateToInterfaceOrientation。注意:爲了向後兼容iOS 4 and 5,仍是須要在你的app裏保留shouldAutorotateToInterfaceOrientation。iphone

for ios 4 and 5, 若是沒有重寫shouldAutorotateToInterfaceOrientation,那麼對於iphone來說,by default是隻支持portrait,不能旋轉。ide

for ios 6, 若是沒有重寫shouldAutorotate and supportedInterfaceOrientations,by default, iphone則是"能夠旋轉,支持非upside down的方向",而ipad是"能夠選擇,支持全部方向"
spa


example 1: for ios 4 and 5, iphone device, 若要"能夠旋轉,支持非upside down的方向",則能夠在view controller裏
.net

[cpp]  view plain copy
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
  2. return (interfaceOrientation != UIDeviceOrientationPortraitUpsideDown);  
  3. }  

example 2: for ios 6, iphone device, 若要「不能旋轉,只支持portait",則能夠在view controller裏

[cpp]  view plain copy
  1. - (BOOL)shouldAutorotate  
  2. {  
  3.     return NO;  
  4. }  

example 3: for ios 6, ipad device, 若要「能夠旋轉,只支持landscape",則能夠在view controller裏

[cpp]  view plain copy
  1. -(NSUInteger)supportedInterfaceOrientations{  
  2.     return UIInterfaceOrientationMaskLandscape;  
  3. }  
  4.   
  5. - (BOOL)shouldAutorotate  
  6. {  
  7.     return YES;  
  8. }  


* 在iOS 4 and 5,都是由具體的view controller來決定對應的view的orientation設置。而在iOS 6,則是由top-most  controller來決定view的orientation設置。orm

舉個例子:你的app的rootViewController是navigation controller "nav", 在」nav"裏的stack依次是:main view -> sub view > sub sub view,而main view裏有一個button會present modal view "modal view".blog

那麼for ios 4 and 5,在ipad裏,若是你要上述view都僅支持橫屏orientation,你須要在上面的main view, sub view, sub sub view, model view裏都添加ip

[cpp]  view plain copy
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
  2. return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight);  
  3. }  

而對於iOS6, 因爲是由top-most controller來設置orientation,所以你在main view, sub view, sub sub view裏添加下面的代碼是沒有任何效果的,而應該是在nav controller裏添加下列代碼。而modal view則不是在nav container裏,所以你也須要在modal view裏也添加下列代碼。

[cpp]  view plain copy
  1. -(NSUInteger)supportedInterfaceOrientations{  
  2.     return UIInterfaceOrientationMaskLandscape;  
  3. }  
  4.   
  5. - (BOOL)shouldAutorotate  
  6. {  
  7.     return YES;  
  8. }  

注意:

*你須要自定義一個UINavigationController的子類for "nav controller",這樣才能夠添加上述代碼。ci

* 和navigation controller相似,tab controller裏的各個view的orientation設置應該放在tab controller裏


for ios6的top-most controller決定orientation設置,致使這樣一個問題:在 top-most controller裏的views沒法擁有不相同的orientation設置。例如:for iphone, 在nav controller裏,你有main view, sub view and sub sub view,前2個都只能打豎,而sub sub view是用來播放video,能夠打橫打豎。那麼在ios 4 and 5裏能夠經過在main view and sub view的shouldAutorotateToInterfaceOrientation裏設置只能打豎,而在sub sub view的shouldAutorotateToInterfaceOrientation設置打豎打橫便可。而在ios 6裏則沒法實現這種效果,由於在main view, sub view and sub sub view的orientation設置是無效的,只可以在nav controller裏設置。那麼你可能想着用下列代碼在nav controller裏控制哪一個view打豎,哪一個view打橫

[cpp]  view plain copy
  1. -(NSUInteger)supportedInterfaceOrientations{  
  2.     if([[self topViewController] isKindOfClass:[SubSubView class]])  
  3.         return UIInterfaceOrientationMaskAllButUpsideDown;  
  4.     else  
  5.         return UIInterfaceOrientationMaskPortrait;  
  6. }  

是的,這樣可使得在main view and sub view裏沒法打橫,而sub sub view橫豎都行。但問題來了,若是在sub sub view時打橫,而後back to sub view,那麼sub view是打橫顯示的!

目前想到的解決方法只能是把sub sub view脫離nav controller,以modal view方式來顯示。這樣就能夠在modal view裏設置打橫打豎,而在nav controller裏設置只打豎。


* 說了那麼多,其實若是你的app的全部view的orientation的設置是統一的,那麼你能夠簡單的在plist file裏設置便可,不用添加上面的代碼。而若是你添加了上面的代碼,就會覆蓋plist裏orientation的設置。


* in iOS 6, 當view controller present時,不會call willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods,只有在發生rotate的時候纔會call

相關文章
相關標籤/搜索