簡體   English   中英

如何允許特定的視圖控制器同時支持橫向和縱向

[英]How to allow a particular view controller to support for both landscape and portrait orientation

我的應用程序是支持iOS 6的基於導航的應用程序。除視圖控制器外,其他所有應用程序將僅支持縱向模式。 僅特定視圖控制器必須同時支持橫向和縱向方向。

我搜索了很多關於此的大量問題,但沒有一個答案適合我。 如果有人知道,請指導我

我在項目->目標->摘要->支持的方向中將方向設置為肖像

首先,如果要為iOS6開發應用程序,則應使用UIViewController文檔中介紹的iOS6方法。 像取向方法shouldAutorotateToInterfaceOrientation在iOS6的已過時,對iOS6的另一種方法是shouldAutoRotate 如果您的應用還支持iOS5,則應僅使用舊方法。

其次,如果您在應用程序中使用UINavigationcontroller且需要具有不同的界面方向,則navigationController可能會弄亂應用程序中的界面方向。 可能的解決方案(對我有用)是實現一個自定義UINavigationController並重寫該自定義UINavigationController類中的界面方向方法,這將使您的viewControllers根據您設置的方向旋轉,因為您的控制器是從UINavigationController推入的。 不要忘記在您的特定viewController中添加這些方法。

CustomNavigationController.h

@interface CustomNavigationController : UINavigationController
@end

CustomNavigationController.m

@implementation CustomNavigationController

//overriding shouldRotate method for working in navController
-(BOOL)shouldAutorotate
{  
  return   [self.topViewController shouldAutorotate];   
}

-(NSUInteger)supportedInterfaceOrientations
{
   return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
   return [self.topViewController preferredInterfaceOrientationForPresentation];
}

我真的認為,正確的方法是將橫向和縱向都設置為受支持的方向,而不要更改不想旋轉的VC上的方向,方法是在屏幕上返回NO。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

如果不應支持傳遞給該方法的方向。

如評論中所述,該方法不再可用。 您應該使用:

-(BOOL) shouldAutorotate

並找出此函數中的當前方向,如果不想旋轉,則返回NO。

不要忘記讓您的應用程序具有不同的方向!

否則,上面沒有任何作用

在“部署信息”部分下的“常規”中的項目目標中進行設置:

在此處輸入圖片說明

暫無
暫無

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

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