iOS 7+ 如何关闭模态视图控制器并强制竖屏方向?

14

我在iOS 7和8上有一个UINavigationController作为我的UIWindow的根视图控制器。从其中一个视图控制器中,我用淡入淡出的方式呈现全屏模态视图控制器。这个模态视图控制器应该能够旋转到所有方向,并且它工作正常。

问题是当设备处于横向方向并且模态视图控制器被取消时。呈现模态视图的视图控制器仅支持纵向方向,并且我确认在 -application:supportedInterfaceOrientationsForWindow: 中返回了UIInterfaceOrientationMaskPortrait。-shouldAutorotate也返回YES。然而,在取消模态后,呈现视图控制器的方向仍然是横向。如何强制其保持纵向方向,同时允许模态视图采用设备的方向?我的代码如下:

AppDelegate:

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

也许我在这里的答案适用于你 https://dev59.com/fWAf5IYBdhLWcg3w0Vi7#25745389 - jules
我今天遇到了同样的问题,这是解决方案:http://stackoverflow.com/a/29560217/1658831 - Pauls
3个回答

7
如果模态控制器在消失之前处于横向方向,则呈现视图控制器可能无法返回原始方向(纵向)。问题是因为AppDelegate中的supportedInterfaceOrientationsForWindow方法在实际解除控制器之前被调用,而呈现的控制器仍然返回横向掩码。
设置一个标志来指示是否将显示(模态)呈现的视图控制器。
- (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];
}

在弹出的模态视图控制器中,根据标志设置方向:当模态视图控制器被呈现时,返回横向。当其被解除显示时,则返回纵向。

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

最后一步 - 从你的AppDelegate调用模态呈现的ViewController来设置其方向。我只是检查当前呈现的ViewController并在其上调用supportedInterfaceOrientations。

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

更多信息请查看此链接

这是关于iOS技术中横向方向的一个视图控制器的说明。

完美解决方案。 (y) - DI Developers

2
这个解决方案适用于iOS 8及以上版本。

问题描述

  1. 应用程序的键窗口具有UINavigationController的子类作为其rootViewController。
  2. 这个NC子类禁止某些界面方向。
  3. NC堆栈中的一些视图控制器(VC1)以模态和全屏方式呈现另一个视图控制器(VC2)。
  4. 此呈现的VC2允许比NC更多的界面方向。
  5. 用户将设备旋转到NC禁止但VC2允许的方向。
  6. 用户关闭了呈现的VC2。
  7. VC1的视图框架不正确。

设置和说明

UINavigationController的子类:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

VC1的初始外观和UI视图堆栈:

初始外观

从VC1呈现VC2(在该示例中为QLPreviewController):

QLPreviewController *pc = [[QLPreviewController alloc] init];
pc.dataSource = self;
pc.delegate = self;
pc.modalPresentationStyle = UIModalPresentationFullScreen;
[self.navigationController presentViewController:pc animated:YES completion:nil];

VC2已呈现并设备旋转为横向:

Presented and rotated

VC2已解除,设备回到纵向模式,但NC堆栈仍处于横向模式:

VC2 dismissed


原因

苹果文档指出:

当您使用presentViewController:animated:completion:方法呈现视图控制器时,UIKit始终管理演示过程。该过程的一部分涉及创建适合给定演示样式的演示控制器。

显然,在处理UINavigationController堆栈时存在错误。


解决方案

可以通过提供自己的过渡委托来绕过此错误。

BTTransitioningDelegate.h

#import <UIKit/UIKit.h>

@interface BTTransitioningDelegate : NSObject <UIViewControllerTransitioningDelegate>

@end

BTTransitioningDelegate.m

#import "BTTransitioningDelegate.h"

static NSTimeInterval kDuration = 0.5;

// This class handles presentation phase.
@interface BTPresentedAC : NSObject <UIViewControllerAnimatedTransitioning>

@end

@implementation BTPresentedAC

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return kDuration;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)context
{
    // presented VC
    UIViewController *toVC = [context viewControllerForKey:UITransitionContextToViewControllerKey];

    // presented controller ought to be fullscreen
    CGRect frame = [[[UIApplication sharedApplication] keyWindow] bounds];
    // we will slide view of the presended VC from the bottom of the screen,
    // so here we set the initial frame
    toVC.view.frame = CGRectMake(frame.origin.x, frame.origin.y + frame.size.height, frame.size.width, frame.size.height);

    // [context containerView] acts as the superview for the views involved in the transition
    [[context containerView] addSubview:toVC.view];

    UIViewAnimationOptions options = (UIViewAnimationOptionCurveEaseOut);

    [UIView animateWithDuration:kDuration delay:0 options:options animations:^{
        // slide view to position
        toVC.view.frame = frame;
    } completion:^(BOOL finished) {
        // required to notify the system that the transition animation is done
        [context completeTransition:finished];
    }];
}

@end


// This class handles dismission phase.
@interface BTDismissedAC : NSObject <UIViewControllerAnimatedTransitioning>

@end

@implementation BTDismissedAC

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return kDuration;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)context
{
    // presented VC
    UIViewController *fromVC = [context viewControllerForKey:UITransitionContextFromViewControllerKey];
    // presenting VC
    UIViewController *toVC = [context viewControllerForKey:UITransitionContextToViewControllerKey];

    // inserting presenting VC's view under presented VC's view
    toVC.view.frame = [[[UIApplication sharedApplication] keyWindow] bounds];
    [[context containerView] insertSubview:toVC.view belowSubview:fromVC.view];

    // current frame and transform of presented VC
    CGRect frame = fromVC.view.frame;
    CGAffineTransform transform = fromVC.view.transform;

    // determine current presented VC's view rotation and assemble
    // target frame to provide naturally-looking dismissal animation
    if (transform.b == -1) {
        // -pi/2
        frame = CGRectMake(frame.origin.x + frame.size.width, frame.origin.y, frame.size.width, frame.size.height);
    } else if (transform.b == 1) {
        // pi/2
        frame = CGRectMake(frame.origin.x - frame.size.width, frame.origin.y, frame.size.width, frame.size.height);
    } else if (transform.a == -1) {
        // pi
        frame = CGRectMake(frame.origin.x, frame.origin.y - frame.size.height, frame.size.width, frame.size.height);
    } else {
        // 0
        frame = CGRectMake(frame.origin.x, frame.origin.y + frame.size.height, frame.size.width, frame.size.height);
    }

    UIViewAnimationOptions options = (UIViewAnimationOptionCurveEaseOut);

    [UIView animateWithDuration:kDuration delay:0 options:options animations:^{
        // slide view off-screen
        fromVC.view.frame = frame;
    } completion:^(BOOL finished) {
        // required to notify the system that the transition animation is done
        [context completeTransition:finished];
    }];
}

@end


@implementation BTTransitioningDelegate

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    return [[BTPresentedAC alloc] init];
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    return [[BTDismissedAC alloc] init];
}

@end

将过渡委托导入呈现VC:

#import "BTTransitioningDelegate.h"

存储一个实例的强引用:

@property (nonatomic, strong) BTTransitioningDelegate *transitioningDelegate;

-viewDidLoad 中实例化:

self.transitioningDelegate = [[BTTransitioningDelegate alloc] init];

在合适的时候调用:

QLPreviewController *pc = [[QLPreviewController alloc] init];
pc.dataSource = self;
pc.delegate = self;
pc.transitioningDelegate = self.transitioningDelegate;
pc.modalPresentationStyle = UIModalPresentationFullScreen;

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

1
我最后选择继承UINavigationController并覆盖它的旋转方法。以下解决方案适用于iOS 7,但我认为在iOS 8 beta 5中存在一个错误,导致在横向模式下取消模态时呈现的视图控制器的视图缩小到屏幕高度的一半。
UINavigationController子类:
- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

@TomerPeled,是的,有。 - asaf am

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接