簡體   English   中英

使用UITabBar在iOS 6中自動旋轉單個UIViewController

[英]Autorotate a single UIViewController in iOS 6 with UITabBar

我有一個僅在Portrait Mode工作的應用程序,但有一個可以顯示視頻的單一視圖,所以我希望該視圖也可以在landscape mode ,但在iOS 6中,我無法弄清楚我是如何做到的,現在我有這個:

在AppDelegate.mi中有:

self.window.rootViewController = myTabBar;

然后在項目摘要中:

在此輸入圖像描述

我發現在iOS 6中檢測視圖旋轉我必須這樣做:

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}

// Tell the system It should autorotate
- (BOOL) shouldAutorotate {
return YES;
}

所以我只在我的UIViewController中插入上面的代碼,我也想在景觀中使用,但不工作,誰知道我怎么能這樣做? 我只是想在顯示視頻時自動旋轉。

首先,您的目標設置應如下所示: 支持的接口方向

在UITabBarController中:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

-(NSUInteger)supportedInterfaceOrientations
{
    if (self.selectedViewController) 
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

在ViewController中:

a)如果你不想輪換:

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

b)如果要旋轉到橫向:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

編輯:

其他解決方案是在AppDelegate中實現此方法:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations; 
}

我會寫評論,但我不能,所以我發布這個作為答案。

這是我的情景:

我的應用程序支持僅在某些視圖上更改方向,我無法弄清楚如何只為我想要的那些做,然后我找到了這個問題並看到了mientus的回答(謝謝你)然后我繼續前進並做了什么他建議哪個是子類UITabBarController並覆蓋這些方法:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{

    NSLog(@"AUTO ROTATE IN CUSTOM TAB BAR");
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}


-(NSUInteger)supportedInterfaceOrientations{

    NSLog(@"supportedInterfaceOrientations IN CUSTOM TAB BAR");

    if (self.selectedViewController)
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate{

    NSLog(@"shouldAutorotate IN CUSTOM TAB BAR");
    return [self.selectedViewController shouldAutorotate];
}

然后在每個視圖控制器內我會有方法來指示我是否想要旋轉。 UITabBarController中的方法被調用但不是我的viewcontroller中的方法因此旋轉仍然發生在我不想要的地方。 然后我將UINavigationController子類化並覆蓋相同的方法,只需對supportedInterfaceOrientation進行此更改,如下所示:

- (NSUInteger)supportedInterfaceOrientations{

NSLog(@"supportedInterfaceOrientations IN CUSTOM NAV BAR CALLING CURRENT VIEW CONTROLLER");
UIViewController* presented = [[self viewControllers] lastObject];
return [presented supportedInterfaceOrientations];

}

這基本上是什么,它獲取當前視圖控制器,然后要求支持的方向,我的視圖控制器中的方法被調用,我可以處理我想要的方向。

您想要旋轉僅限縱向視圖子視圖的視圖嗎? 通常視圖旋轉行為繼承自rootviewcontroller。 然后,如果你在rootviewcontroller中的shouldAutorotate中返回NO,則在每個單一的underview中停止旋轉。

我建議用這種方式拆分你的架構:

rootViewController - > supportedInterfaceOrientations = Portrait&shouldAutorotate = YES NORotationViewControllers - > supportedInterfaceOrientations = Portrait&shouldAutorotate = YES rotationViewControllers - > supportedInterfaceOrientations = All&shouldAutorotate = YES

如果您還沒有閱讀過,請查看: 支持多種接口方向

暫無
暫無

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

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