簡體   English   中英

在iOS7和iOS8中自動旋轉

[英]Auto rotate in iOS7 and iOS8

我正在開發一個縱向模式的應用程序。 但是我希望一個視圖控制器既可以橫向顯示也可以縱向顯示。

我嘗試了以下代碼,但它不起作用(未調用)

- (BOOL) shouldAutorotate
{
    return NO;
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationMaskPortrait;
}

首先,您需要在plist中設置應用程序支持的所有方向,這可以在項目的“常規”標簽中的“部署信息”下進行,例如: 在此處輸入圖片說明

然后,您可以使用supportedInterfaceOrientations方法,

我假設您以模態呈現視圖控制器,因此只需在呈現的viewController上重寫它即可,該視圖控制器僅需縱向使用:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

並在顯示的viewController中也應支持橫向,請使用:(或您想要的任何方向蒙版)

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

PS-模態顯示的viewController和推入navigationController堆棧的viewController有不同的行為:

  • modalViewController將調用其自己的supportedInterfaceOrientations ,並將支持這些方向
  • pushedViewController將調用其navigationController supportedInterfaceOrientations ,並支持這些方向。

因此,如果以模態形式顯示viewController,則需要重寫其自己的supportedInterfaceOrientations ,但是如果按下此viewController,則需要在navigationController中設置一些BOOL屬性,以便它將知道要支持的方向。 我建議您以模態形式顯示此viewController,對於不同的設備方向使用modalViewController更自然。

PS#2 :關於shouldAutorotate :如果返回“ NO”,則不調用supportedInterfaceOrientations ,因此返回“ YES”。 它只說設備旋轉時是否自動旋轉。 如果返回“ NO”,則需要顯式旋轉viewController。

好吧,我希望我能幫助並且沒有寫出一個完全沒有考慮到你所問的答案的答案... :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM