簡體   English   中英

使用UITabBarController和/或UINavigationController時,僅將某些視圖控制器限制/強制為特定方向

[英]Restrict/Force only some view controllers to specific orientations when using UITabBarController and/or UINavigationController

我在網上搜尋了很多東西,但是找不到確切的答案。 當將一個UIViewController嵌入到UINavigationController本身是UITabBarController一部分時,只有一種UIViewController支持橫向模式的最佳方法是什么?

像這樣的大多數解決方案都建議覆蓋

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

AppDelegate 這在某種程度上是可行的,但是當從唯一一個支持橫向模式的ViewController返回時,所有其他(非橫向ViewController)也將處於橫向。 他們不保持縱向。

我見過應用程序能正確實現這一點,所以我知道一定有可能實現。 例如,電影播放器​​應用程序通常僅是縱向的,但是實際的電影播放器​​視圖以強制橫向模式模態顯示。 解散模態視圖控制器后,基礎視圖控制器仍正確以縱向放置,

有什么提示嗎?

經過大量研究,這是我的結論:

首先,我嘗試實施

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window;

如此處所述 AppDelegate中運行,適用於大多數情況。 但是除了感到“ hacky”外,還有一個嚴重的陷阱:例如,當顯示AVPlayerViewController時,模式顯示的視圖控制器的變通方法(請參閱“模式控制器的小問題”一節)由於其實現了自己的關閉而失效方法,您無法將其設置為self.isPresented (不幸的是, viewWillDisappear:為時已晚)。

因此,我采用了為UITabBarControllerUINavigationController使用子類的另一種方法,它感覺更干凈,但也更加冗長:

CustomNavigationController

CustomNavigationController.h

#import <UIKit/UIKit.h>

@interface CustomNavigationController : UINavigationController

@end

CustomNavigationController.m

#import "CustomNavigationController.h"

@implementation CustomNavigationController

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

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

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

@end

CustomTabBarController

CustomTabBarController.h

#import <UIKit/UIKit.h>

@interface CustomTabBarController : UITabBarController

@end

CustomTabBarController.m

#import "CustomTabBarController.h"

@implementation CustomTabBarController

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

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

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

@end

之后,只需將以下代碼添加到UIViewControllers

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    // Presents the `UIViewController` in landscape orientation when it is first displayed 
    return UIInterfaceOrientationLandscapeLeft;
}

- (NSUInteger)supportedInterfaceOrientations {
    // Allows all other orientations (except upside-down)
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

現在,您可以基於每個視圖控制器確定首選和支持的方向。 請注意,我沒有在我的視圖控制器中實現shouldAutorotate ,因為它默認為YES,這是您將視圖控制器強制設置為某個方向時想要的(是的,它們應該自動旋轉為唯一受支持的方向)。

調用鏈如下所示:

  • 要求CustomTabBarController作為窗口的rootViewController ,以獲取受支持/首選的方向
  • CustomTabBarController依次詢問其selectedViewController (當前選定選項卡的視圖控制器),恰好是我的CustomNavigationController
  • 所述CustomNavigationController詢問嵌入topViewController ,這是最后的實際UIViewController實現上述方法的

您仍然需要在構建目標中允許所有設備方向。 當然,您需要更新情節提要/ xib /類以使用這些子類,而不是標准的UINavigationControllerUITabBarController類。

這種方法的唯一缺點是,至少就我而言,我必須將這些方法添加到我的所有視圖控制器中,以使大多數視圖控制器僅是縱向的並且具有橫向功能。 但是恕我直言,這絕對值得!

您應該將上面的代碼編寫到UINavigationController子類中。 您應該定義應用程序方向,並使用if語句分隔視圖控制器方向。

-(BOOL)shouldAutorotate {

    if ([[self.viewControllers lastObject] isKindOfClass:[UIViewController class]] ) {

        return [[self.viewControllers lastObject] shouldAutorotate];
    }

    return NO;

}
- (NSUInteger)supportedInterfaceOrientations {

    if ([[self.viewControllers lastObject] isKindOfClass:[UIViewController class]]) {

        return [[self.viewControllers lastObject] supportedInterfaceOrientations];
    }

    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {


    return UIInterfaceOrientationPortrait;

}

暫無
暫無

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

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