fixed orientation for some view controllers

后端 未结 1 384
死守一世寂寞
死守一世寂寞 2021-01-27 18:59

In my app i\'ve both tabbar and navigationBar. rootview controller tabbar and tabbar has 4 navigation controller.

I want to make some viewcontrollers to be portrait only

相关标签:
1条回答
  • 2021-01-27 19:17

    I've solved this problem and answering so that if someone face same problem they can get a help.

    shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
    

    The above methods don't get called of a viewcontroller if they are inside any tabbarcontroller of navigationcontroller. If these methods are declared inside tabbarcontroller or navigation controller then they will get called. In my case the viewcontrollers was inside navigationcontroller and the navigation controllers are inside a tabbarcontroller.

    For solving this I make a class FixedOrientationTab, it is a subclass of UITabBarController, and a navigation class OrientationEnabledNavigation, it is a subclass of UINavigationController. Then I implemented shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation methods inside FixedOrientationTab and OrientationEnabledNavigation.

    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
    

    Then if you want to use navigation controller in your project then use OrientationEnabledNavigation and for tabbar FixedOrientationTab. After that if you implement shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation these method inside your viewcontroller then they will get called.

    Hope this helps.. :)

    0 讨论(0)
提交回复
热议问题