为什么我不能强迫横向使用时的UINavigationController?(Why can'

2019-07-19 03:33发布

我发现很多问题,迫使UIViewController的景观为默认: 如何强制一个UIViewController为纵向在iOS 6中并尝试这样的:

- (BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

但是当我使用的UIViewController内的UINavigationController,我不能强迫到landscape.Please帮助!

*工作不正常

- (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;
}

*工作OK:

self.window.rootViewController = self.viewController;

Answer 1:

要做到这一点的最佳方式是创建扩展的UINavigationController和写扩展的类里面的定位功能。

类声明:

@interface MyNavigationControllerViewController : UINavigationController

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

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

并使用你的延伸,就像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

所以,你的应用程序委托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;
}

只有这样,你可以用相同的导航控制器两种不同的观点提供了不同的方向重新加载这一领域的空白导航控制器本身。 所以,如果你加两个方法

- (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];
}

并推动和流行的看法后调用这些功能。

注: - 我不知道它是否是一个很好的方式或坏的方式。



Answer 2:

你需要去到您的plist文件和设置的方位。 你也可以这样做对你的项目文件。 plist中的方位设置将覆盖虽然所有。

在你的plist您可以设置两个方向的选择向左景观按钮和景观右。



文章来源: Why can't I force landscape orientation when use UINavigationController?