0

在我的應用程序中,我使用導航控制器來管理視圖。我的登錄頁面支持縱向和橫向視圖。當用戶登錄我的第二個視圖是家庭,它只支持橫向模式。我想要做的是當用戶使用縱向視圖主頁登錄到家時,即使縱向顯示設備,也應該以橫向視圖顯示。用戶在縱向視圖時強制加載橫向視圖

因此,我所做的是將狀態欄方向更改爲橫向整理主頁的viewWillAppear方法,如下所示;

-(void)viewWillAppear:(BOOL)animated{ 

     [super viewWillAppear:animated]; 
     [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:NO]; 
UIDeviceOrientation orien = [[UIDevice currentDevice] orientation]; 
    } 

還我重寫shouldAutorotateToInterfaceOrientation如下

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 

return YES; 

} 

我的問題是,即使在狀態欄改爲景觀我UIViewController(家)是保持在橫向模式。當我調試時,我發現即使將狀態欄方向改爲橫向,[[UIDevice currentDevice] orientation]也會返回縱向。我整天都在上網。並且實施許多由其他方案提供的解決方案,但我整天浪費了。有人可以指導我解決這些問題。

+0

你要在橫向模式或某些屏幕的整個應用程序? – 2013-03-18 12:53:01

回答

0

Apple不希望您強制設備的方向。雖然有一個技巧。 不幸的是,我無法訪問我的代碼。

1. Your app in general supports all orientations. 
2. All view controllers only return their supported interface orientation in their overwrites respectivly (in supportedInterfaceOrientations). 
3. All view controllers return YES in shouldAutorotateToInterfaceOrientation only for their supported orientations. 

這很好。但它仍然需要用戶實際旋轉設備。否則整個方向改變機制將不會被調用。 現在,當您要強制更改方向,請執行以下操作:

4. Use setStatusBarOrientation to set the orientation before the next view controller is displayed. 
That alone would not do anything. Plus it would not take any effect if the next view controller is pushed. It would work fine only when the next view controller is presented modally. 
5a. So if you want to present the rotated view controller modally, then do it. 
5b. If you still need to push it then: 
5b1. Create an empty UIViewController instance. alloc/init will do. 
5b2. Present it modally 
5b3. Dismiss it modally 
Now, the new view controller was not even visible to the user but the device - here comes the magic - is rotated now. 
5c4. Next push the view controller that you want to display roated. 

,反之亦然回來的路上:)

所有當你使用一個標籤欄上面的問題變得更爲複雜。你使用標籤欄嗎? 我設法得到一個標籤欄,我不得不繼承覆蓋其旋轉方法。在一個沒有標籤欄的應用程序中,我將UIApplication(!)分類爲UIApplication(!),但不要忘記這是真正需要的,或者我是爲了方便(而不是將更改添加到50+視圖控制器中)。但原則上上面是這樣做的伎倆。

PS:你會找到一個更具有的相關詳細代碼樣本一起回答這裏: Presenting Navigation Controller in Landscape mode is not working ios 6.0

1

你只需要像: -

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 


     return YES; 
    } 
    else 
    { 
     return NO; 
    } 
} 

特定類要打開景觀iOS6的只有

: -

-

(BOOL)shouldAutorotate { 
    return YES; 
} 
- (NSUInteger)supportedInterfaceOrientations { 

    return UIInterfaceOrientationMaskLandscape; 
} 
+0

該方法在iOS 6中已棄用。 – omz 2013-03-18 12:54:31

+0

檢查我的更新回答 – 2013-03-18 12:56:06

+0

原則上罰款。但它仍然需要用戶實際旋轉設備。 – 2013-03-18 13:09:14

0

你可以試試與

- (NSUInteger)supportedInterfaceOrientations { 
     return UIInterfaceOrientationMaskPortrait; 
    } 

- (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)toInterfaceOrientation { 
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait); 
} 

或嘗試呈現它作爲一個模式,而不是推動它。

相關問題