简体   繁体   English

在UINavigationViewController上推送(推送segue)视图后,方向不会改变

[英]Orientation doesn't change after Pushing (push segue) a View on the UINavigationViewController

I have embedded a UIViewController in a UINavigationController . 我在UINavigationController嵌入了一个UIViewController The orientation of the view of this controller is set to Portait. 此控制器的视图方向设置为Portait。 When I push a new view on this UIViewController , which is landscape only, the new view is being shown portrait as well, instead of it's orientation landscape. 当我在这个仅仅是风景的UIViewController上推新视图时,新视图也会显示为纵向,而不是它的方向横向。

I have tried to subclass the UINavigationController and added the following methods like this: 我试图将UINavigationController子类化并添加如下方法:

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

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

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

In the rootViewController (LoginViewController) I did this: 在rootViewController(LoginViewController)中我这样做了:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

In the pushViewController (A custom ViewController) I did this: 在pushViewController(自定义ViewController)中我这样做了:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;
}

I'm using a storyboard and a push segue between them. 我正在使用故事板和它们之间的推动。 I know that the problem lies in the push segue which leads to an taking over of the orientation of the topviewcontroller which is portrait and the pushViewController is landscape. 我知道问题在于推送segue导致接管topviewcontroller的方向,这是纵向和pushViewController是风景。 Does anyboy know workarounds? anyboy是否知道变通办法?

Any help is thankfully appreciated. 感谢任何帮助。 Else I should drop the navVC and perform a modal segue. 否则我应该放弃navVC并执行模态segue。

KR KR

Try this code : 试试这段代码:

In AppDelegate.m class write below code. 在AppDelegate.m类中编写下面的代码。

#pragma mark Orientation Code
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations;
}

And next if you don't want orientation of the particular class for example 接下来如果你不想要特定类的方向

Stop orientation viewController.m 停止方向viewController.m

#pragma mark Orientation
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

final thing to change project device orientation of project Target. 最后改变项目目标的项目设备方向。

Ex : Project TARGETS --> Device Orientation -- > select All (Portrait, UpSide Down, Landscape Left, Landscape Right) 例如:项目目标 - >设备方向 - >全选(纵向,向上,向左,向右)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM