iOS 7+关闭模态视图控制器和强制纵向方向 [英] iOS 7+ Dismiss Modal View Controller and Force Portrait Orientation

查看:158
本文介绍了iOS 7+关闭模态视图控制器和强制纵向方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UINavigationController作为我在iOS 7和iOS 8上的UIWindow的根视图控制器。从它的一个视图控制器,我提出了一个具有交叉溶解演示风格的全屏模态视图控制器。这个模态视图控制器应该能够旋转到所有方向,并且它工作正常。

I have a UINavigationController as the root view controller of my UIWindow on iOS 7 and iOS 8. From one of its view controllers, I present a fullscreen modal view controller with a cross-dissolve presentation style. This modal view controller should be able to rotate to all orientations, and it works fine.

问题是当设备以横向方向保持并且模态视图控制器被解除时。呈现模态的视图控制器仅支持纵向方向,并且我已确认UIInterfaceOrientationMaskPortrait返回到-application:supportedInterfaceOrientationsForWindow:。 -shouldAutorotate也返回YES。然而,在解除模态之后,呈现视图控制器的方向仍然是风景。在允许模态采取设备方向的同时,如何强制它保持纵向方向?我的代码如下:

The problem is when the device is held in a landscape orientation and the modal view controller is dismissed. The view controller which presented the modal only supports portrait orientation, and I've confirmed that UIInterfaceOrientationMaskPortrait is returned to -application:supportedInterfaceOrientationsForWindow:. -shouldAutorotate returns YES, as well. However, the orientation of the presenting view controller, after dismissing the modal, remains landscape. How can I force it to remain in portrait orientation while allowing the modal to take the orientation of the device? My code follows:

应用代表:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        UINavigationController *navigationController = (UINavigationController *)self.deckController.centerController;
        NSArray *viewControllers = [navigationController viewControllers];
        UIViewController *top = [viewControllers lastObject];

        if (top && [top presentedViewController]) {
            UIViewController *presented = [top presentedViewController];
            if ([presented respondsToSelector:@selector(isDismissing)] && ![(id)presented isDismissing]) {
                top = presented;
            }
        }

        return [top supportedInterfaceOrientations];
    }

    return (UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight);
}

呈现视图控制器:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

模态视图控制器:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskPortrait);
}


推荐答案

如果模态控制器在解雇前的横向方向,呈现的ViewController可能无法返回到原点方向(纵向)。问题是因为AppDelegate supportedInterfaceOrientationsForWindow方法在实际解除控制器之前被调用,并且呈现的控制器检查仍然返回Landscape掩码。

If the modal controller was in landscape orientation before dismissal, the presenting ViewController may not return to the origin orientation (portrait). The problem is because the AppDelegate supportedInterfaceOrientationsForWindow method is called before the controller is actually dismissed and the presented controller check still returns Landscape mask.

设置一个标志以指示是否(模态) )显示视图控制器将显示与否。

Set a flag to indicate whether the (modal) presented view controller will be displayed or not.

- (void)awakeFromNib // or where you instantiate your ViewController from
{
    [super awakeFromNib];
    self.presented = YES;
}

- (IBAction)exitAction:(id)sender // where you dismiss the modal
{
    self.presented = NO;
    [self dismissViewControllerAnimated:NO completion:nil];
}

在提供的模式中,ViewController根据标志设置方向:提供了模态ViewController - 返回Landscape。当它被解雇然后返回肖像

And in the modal presented ViewController set the orientation according to the flag: When the modal ViewController is presented - return Landscape. When it is dismissed then return portrait

- (NSUInteger)supportedInterfaceOrientations
{
    if ([self isPresented]) {
        return UIInterfaceOrientationMaskLandscape;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

最后一步 - 从AppDelegate调用模态呈现的ViewController因为它的方向。我只是检查当前呈现的视图控制器,并呼吁它

Last step - from your AppDelegate call the modal presented ViewController for its orientation. I am just checking the currently presented ViewController and call the supportedInterfaceOrientations on it

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientationMask = UIInterfaceOrientationMaskPortrait;

    UIViewController *currentVC = self.window.rootViewController.presentedViewController; // gets the presented VC
    orientationMask = [currentVC supportedInterfaceOrientations];

    return orientationMask;
}

有关详细信息,请查看此链接

For more info check this link

这篇关于iOS 7+关闭模态视图控制器和强制纵向方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆