简体   繁体   English

如果UIViewController的父级处于纵向模式,如何以横向模式加载UIViewController?

[英]How to load a UIViewController in landscape mode if its parent is in Portrait mode?

I want to load a ViewController in landscape mode from a portrait parent. 我想从肖像父级以横向模式加载ViewController。 Parent is got fixed for portrait, it wont change its orientation, however the child view controller whichever is got pushed from the parent also loading portrait initially irrespective of the device orientation (if we rotate the device few more times then the orientation set properly for childs). 家长固定为肖像,它不会改变其方向,但是无论从设备的方向如何(无论我们将设备旋转几次,然后为孩子正确设置的方向),从父母那里推动的子视图控制器都会首先加载人像)。 So how to load child properly in initial time itself. 因此,如何在初始时间本身正确加载子级。

or 要么

Is there any way I can set the orientation programatically, so that I can use it in ViewWillAppear method. 有什么办法可以以编程方式设置方向,以便可以在ViewWillAppear方法中使用它。 Thanx. 谢谢

In your child UIViewController set these two methods: 在您的子UIViewController中设置以下两个方法:

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

In parent UIViewController do: 在父UIViewController中执行以下操作:

[self.navigationController presentViewController:controller animated:YES completion:nil];

instead of: 代替:

[self.navigationController pushViewController:controller animated:YES];

If you want to present navigation bar in child: 如果要在子项中显示导航栏:

DetailViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
controller.title = @"Child";

You'll have to subclass UINavigationController with the two methods mentioned above instead in the child view controller. 您必须在子视图控制器中使用上述两个方法将UINavigationController子类化。

MyNavigationController *nav = [[MyNavigationController alloc] initWithRootViewController:controller];

[self.navigationController presentViewController:nav animated:YES completion:nil];

If i got it right, you just add this methods to your child view controller 如果我做对了,您只需将此方法添加到您的子视图控制器中

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft / Right;
}

To present your view use 呈现您的视图用途

[self presentViewController:viewController...]

And your view is presented in Landscape mode. 并且您的视图以横向模式显示。

Use this in view did appear or will appear, 使用此视图确实出现了或将要出现,

[[UIDevice currentDevice]performSelector:@selector(setOrientation:) withObject:(__bridge id)((void *)UIInterfaceOrientationLandscapeRight)];

Before that Enable all 4 orientations in your .plist 在此之前,在.plist中启用所有4个方向

Add this on AppDelegate.m 在AppDelegate.m上添加

- (NSUInteger)application:(UIApplication*)application
supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
    UIViewController *cont=self.vc;

    if([cont isKindOfClass:[YourClass class]])
    {
         NSUInteger orientations =  UIInterfaceOrientationMaskLandscapeRight;

        NSLog(@"Landscape");

        return orientations;
   }

    NSUInteger orientations = UIInterfaceOrientationMaskPortrait;

    return orientations;
} 

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

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