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

时间:2013-06-12 10:19:18

标签: iphone ios cocoa-touch uiviewcontroller uiinterfaceorientation

我想从纵向父级加载横向模式的ViewController。父对象是固定的,它不会改变它的方向,但是无论设备方向如何,如果我们将设备旋转几次,然后为儿童正确设置方向,则从父母推送的子视图控制器也最初加载肖像。 )。那么如何在初始时间内正确加载孩子。

有没有办法可以编程方式设置方向,以便我可以在ViewWillAppear方法中使用它。  感谢名单。

4 个答案:

答案 0 :(得分:3)

在你的孩子UIViewController中设置这两个方法:

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

在父UIViewController中执行:

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

而不是:

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

如果您想在儿童中显示导航栏:

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

您必须使用上面提到的两种方法在子视图控制器中继承UINavigationController。

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

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

答案 1 :(得分:3)

答案 2 :(得分:0)

如果我做对了,你只需将这些方法添加到子视图控制器

- (BOOL)shouldAutorotate {
    return NO;
}

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

使用

显示您的观点
[self presentViewController:viewController...]

您的视图以横向模式显示。

答案 3 :(得分:0)

在视图中使用它确实出现或将出现,

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

在此之前启用.plist中的所有4个方向

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