为什么我不能在使用UINavigationController时强制使用景观方向?

时间:2022-07-03 21:09:32

I find many question to force UIViewController to landscape as default: How to force a UIViewController to Portrait orientation in iOS 6 And try this:

我发现很多问题迫使UIViewController到landscape作为默认:如何强制UIViewController到ios6的竖屏方向试试这个:

- (BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

But when I use UIViewController inside UINavigationController, I can't force to landscape.Please help!

但是当我在UINavigationController中使用UIViewController时,我不能强制到landscape。请帮助!

*Work NOT ok

*工作不可以

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    [navControl setNavigationBarHidden:YES];
    self.window.rootViewController = navControl;

    [self.window makeKeyAndVisible];
    return YES;
}

*Work OK:

*工作好:

self.window.rootViewController = self.viewController;

2 个解决方案

#1


4  

Best way to do this is the create extend UINavigationController and write your orientation function inside the extended class.

最好的方法是创建扩展UINavigationController并在扩展类中编写方向函数。

Class Declaration:

类声明:

@interface MyNavigationControllerViewController : UINavigationController

@property(nonatomic, assign) UIInterfaceOrientation orientation;
@property(nonatomic, assign) NSUInteger supportedInterfaceOrientatoin;
@end

Implementation of MyNavigationController

实现MyNavigationController

@implementation MyNavigationController

@synthesize supportedInterfaceOrientatoin = _supportedInterfaceOrientatoin;

@synthesize orientation = _orientation;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _supportedInterfaceOrientatoin = UIInterfaceOrientationMaskLandscape;
        _orientation = UIInterfaceOrientationLandscapeLeft;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
    return _supportedInterfaceOrientatoin;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return self.orientation;
}

@end

and use your extended like MyNavigationController as navigation controller to your rootviewcontroller.

使用扩展的MyNavigationController作为rootviewcontroller的导航控制器。

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) MyNavigationControllerViewController *myNavController;

- (void) reloadAppDelegateRootViewControllerLandscape;
- (void) reloadAppDelegateRootViewController;
@end

So your application delegate didfinishlounchingwithoptions code will be as follow.

因此,您的应用程序委托didfinishlounchingwithoptions代码将如下所示。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    self.myNavController = [[MyNavigationController alloc] initWithRootViewController:self.viewController];
    [self.myNavController setNavigationBarHidden:YES];
    self.window.rootViewController = self.myNavController;

    [self.window makeKeyAndVisible];
    return YES;
}

Only way you can provide different orientation for two different view with same navigation controller isto reload the navigation controller itself. So if you add two methods

只有在同一个导航控制器中,你才能为两个不同的视图提供不同的方向,从而重新加载导航控制器本身。如果你添加了两种方法。

- (void) reloadAppDelegateRootViewController{
    [[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
    [(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationPortrait];
    [(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskAll];
    [[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}

- (void) reloadAppDelegateRootViewControllerLandscape{
    [[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
    [(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationLandscapeLeft];
    [(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskLandscape];
    [[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}

and call these function after pushing and pop views.

在推送和弹出视图之后调用这些函数。

Note:- I don't know whether it is a good way or bad way.

注意:-我不知道这是好是坏。

#2


-3  

You need to go to your plist file and set the orientations. You can also do this on your project file. The plist orientations settings will override all though.

你需要进入你的plist文件并设置方向。您也可以在您的项目文件中这样做。plist方向设置将覆盖所有的设置。

In your plist you can set both orientation options to landscape button left and landscape button right.

在您的plist中,您可以将两种方向选项分别设置为左横向按钮和右横向按钮。

#1


4  

Best way to do this is the create extend UINavigationController and write your orientation function inside the extended class.

最好的方法是创建扩展UINavigationController并在扩展类中编写方向函数。

Class Declaration:

类声明:

@interface MyNavigationControllerViewController : UINavigationController

@property(nonatomic, assign) UIInterfaceOrientation orientation;
@property(nonatomic, assign) NSUInteger supportedInterfaceOrientatoin;
@end

Implementation of MyNavigationController

实现MyNavigationController

@implementation MyNavigationController

@synthesize supportedInterfaceOrientatoin = _supportedInterfaceOrientatoin;

@synthesize orientation = _orientation;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _supportedInterfaceOrientatoin = UIInterfaceOrientationMaskLandscape;
        _orientation = UIInterfaceOrientationLandscapeLeft;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
    return _supportedInterfaceOrientatoin;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return self.orientation;
}

@end

and use your extended like MyNavigationController as navigation controller to your rootviewcontroller.

使用扩展的MyNavigationController作为rootviewcontroller的导航控制器。

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) MyNavigationControllerViewController *myNavController;

- (void) reloadAppDelegateRootViewControllerLandscape;
- (void) reloadAppDelegateRootViewController;
@end

So your application delegate didfinishlounchingwithoptions code will be as follow.

因此,您的应用程序委托didfinishlounchingwithoptions代码将如下所示。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    self.myNavController = [[MyNavigationController alloc] initWithRootViewController:self.viewController];
    [self.myNavController setNavigationBarHidden:YES];
    self.window.rootViewController = self.myNavController;

    [self.window makeKeyAndVisible];
    return YES;
}

Only way you can provide different orientation for two different view with same navigation controller isto reload the navigation controller itself. So if you add two methods

只有在同一个导航控制器中,你才能为两个不同的视图提供不同的方向,从而重新加载导航控制器本身。如果你添加了两种方法。

- (void) reloadAppDelegateRootViewController{
    [[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
    [(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationPortrait];
    [(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskAll];
    [[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}

- (void) reloadAppDelegateRootViewControllerLandscape{
    [[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
    [(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationLandscapeLeft];
    [(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskLandscape];
    [[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}

and call these function after pushing and pop views.

在推送和弹出视图之后调用这些函数。

Note:- I don't know whether it is a good way or bad way.

注意:-我不知道这是好是坏。

#2


-3  

You need to go to your plist file and set the orientations. You can also do this on your project file. The plist orientations settings will override all though.

你需要进入你的plist文件并设置方向。您也可以在您的项目文件中这样做。plist方向设置将覆盖所有的设置。

In your plist you can set both orientation options to landscape button left and landscape button right.

在您的plist中,您可以将两种方向选项分别设置为左横向按钮和右横向按钮。