2025-02-17

知識點:

*UIViewController的shouldAutorotateToInterfaceOrientation方法被deprecated。在ios6裡,是使用supportedInterfaceOrientations and shouldAutorotate 2個方法來代替shouldAutorotateToInterfaceOrientation。註意:為瞭向後兼容iOS 4 and 5,還是需要在你的app裡保留shouldAutorotateToInterfaceOrientation。

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

for ios 6, 如果沒有重寫shouldAutorotate and supportedInterfaceOrientations,by default, iphone則是"可以旋轉,支持非upside down的方向",而ipad是"可以選擇,支持所有方向"

example 1: for ios 4 and 5, iphone device, 若要"可以旋轉,支持非upside down的方向",則可以在view controller裡
[cpp]
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
return (interfaceOrientation != UIDeviceOrientationPortraitUpsideDown); 

example 2: for ios 6, iphone device, 若要“不能旋轉,隻支持portait",則可以在view controller裡
[cpp] 
– (BOOL)shouldAutorotate 

    return NO; 

example 3: for ios 6, ipad device, 若要“可以旋轉,隻支持landscape",則可以在view controller裡
[cpp] 
-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskLandscape; 

 
– (BOOL)shouldAutorotate 

    return YES; 

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

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

那麼for ios 4 and 5,在ipad裡,如果你要上述view都僅支持橫屏orientation,你需要在上面的main view, sub view, sub sub view, model view裡都添加

[cpp] 
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight); 

而對於iOS6, 由於是由top-most controller來設置orientation,因此你在main view, sub view, sub sub view裡添加下面的代碼是沒有任何效果的,而應該是在nav controller裡添加下列代碼。而modal view則不是在nav container裡,因此你也需要在modal view裡也添加下列代碼。
[cpp] 
-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskLandscape; 

 
– (BOOL)shouldAutorotate 

    return YES; 

註意:
*你需要自定義一個UINavigationController的子類for "nav controller",這樣才可以添加上述代碼。

* 和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] 
-(NSUInteger)supportedInterfaceOrientations{ 
    if([[self topViewController] isKindOfClass:[SubSubView class]]) 
        return UIInterfaceOrientationMaskAllButUpsideDown; 
    else 
        return UIInterfaceOrientationMaskPortrait; 

是的,這樣可以使得在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

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *