【问题标题】:fixed orientation for some view controllers某些视图控制器的固定方向
【发布时间】:2014-02-13 07:32:08
【问题描述】:

在我的应用程序中,我有标签栏和导航栏。 rootview 控制器 tabbar 和 tabbar 有 4 个导航控制器。

我想让一些视图控制器仅是纵向的。这可能是一个常见问题,但我已经尝试了足够多,但我无法解决这个问题。

如何为某些视图控制器设置纵向?

【问题讨论】:

    标签: ios7 uinavigationcontroller uitabbarcontroller uiinterfaceorientation


    【解决方案1】:

    我已经解决了这个问题并回答了,如果有人遇到同样的问题,他们可以得到帮助。

    shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
    

    如果上述方法位于导航控制器的任何选项卡控制器中,则视图控制器不会调用它们。如果这些方法在 tabbarcontroller 或导航控制器中声明,那么它们将被调用。在我的例子中,viewcontrollers 在navigationcontroller 里面,而navigation controllers 在tabbarcontroller 里面。

    为了解决这个问题,我做了一个FixedOrientationTab类,它是UITabBarController的子类,还有一个导航类OrientationEnabledNavigation,它是UINavigationController的子类。然后我在 FixedOrientationTab 和 OrientationEnabledNavigation 中实现了shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation 方法。

    OrientationEnabledNavigation.h

    #import <UIKit/UIKit.h>
    
    @interface OrientationEnabledNavigation : UINavigationController
    
    @end
    

    OrientationEnabledNavigation.m

    #import "OrientationEnabledNavigation.h"
    
    @interface OrientationEnabledNavigation ()
    
    @end
    
    @implementation OrientationEnabledNavigation
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (BOOL)shouldAutorotate
    {
        return [self.topViewController shouldAutorotate];
    //    return NO;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return [self.topViewController preferredInterfaceOrientationForPresentation];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    FixedOrientationTab.h

    #import <UIKit/UIKit.h>
    
    @interface FixedOrientationTab : UITabBarController
    
    @end
    

    FixedOrientationTab.m

    #import "FixedOrientationTab.h"
    
    @interface FixedOrientationTab ()
    
    @end
    
    @implementation FixedOrientationTab
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (BOOL)shouldAutorotate
    {
        return [self.selectedViewController shouldAutorotate];
        //    return NO;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return [self.selectedViewController supportedInterfaceOrientations];
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return [self.selectedViewController preferredInterfaceOrientationForPresentation];
    }
    
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    然后,如果您想在项目中使用导航控制器,请使用 OrientationEnabledNavigation 和标签栏 FixedOrientationTab。之后,如果您在视图控制器中实现shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation 这些方法,那么它们将被调用。

    希望这会有所帮助.. :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 2012-06-11
      • 1970-01-01
      相关资源
      最近更新 更多