在iOS 13中,模态视图控制器在呈现时有一个新的行为。

现在它不是全屏默认情况下,当我向下滑动时,应用会自动解除视图控制器。

我怎么能防止这种行为,并回到旧的全屏模态vc?

谢谢


当前回答

override modalPresentationStyle将修复使用或不使用编码器创建的UIViewControllers的样式。

优点:

你放置它的唯一位置。 不需要知道应该设置哪个init或awake方法

劣势:

你不能从外部改变它,像接口构建器或配置从代码

解决方案:

override var modalPresentationStyle: UIModalPresentationStyle {
    get { .fullScreen }
    set { }
}

其他回答

我在启动屏幕后的初始视图中遇到了这个问题。修复我,因为我没有一个segue或逻辑定义是切换演示从自动到全屏如下所示:

我通过使用方法swizzling(Swift 4.2)实现了它:

创建UIViewController扩展,如下所示

extension UIViewController {

    @objc private func swizzled_presentstyle(_ viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?) {

        if #available(iOS 13.0, *) {
            if viewControllerToPresent.modalPresentationStyle == .automatic || viewControllerToPresent.modalPresentationStyle == .pageSheet {
                viewControllerToPresent.modalPresentationStyle = .fullScreen
            }
        }

        self.swizzled_presentstyle(viewControllerToPresent, animated: animated, completion: completion)
    }

     static func setPresentationStyle_fullScreen() {

        let instance: UIViewController = UIViewController()
        let aClass: AnyClass! = object_getClass(instance)

        let originalSelector = #selector(UIViewController.present(_:animated:completion:))
        let swizzledSelector = #selector(UIViewController.swizzled_presentstyle(_:animated:completion:))

        let originalMethod = class_getInstanceMethod(aClass, originalSelector)
        let swizzledMethod = class_getInstanceMethod(aClass, swizzledSelector)
        if let originalMethod = originalMethod, let swizzledMethod = swizzledMethod {
        method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

在AppDelegate中,在application:didFinishLaunchingWithOptions中:调用swizzling代码通过调用:

UIViewController.setPresentationStyle_fullScreen()

这是我在objective - c中使用类别的修复版本。使用这种方法,你将拥有默认的UIModalPresentationStyleFullScreen行为,直到另一个显式设置。

#import "UIViewController+Presentation.h"
#import "objc/runtime.h"

@implementation UIViewController (Presentation)

- (void)setModalPresentationStyle:(UIModalPresentationStyle)modalPresentationStyle {
    [self setPrivateModalPresentationStyle:modalPresentationStyle];
}

-(UIModalPresentationStyle)modalPresentationStyle {
    UIModalPresentationStyle style = [self privateModalPresentationStyle];
    if (style == NSNotFound) {
        return UIModalPresentationFullScreen;
    }
    return style;
}

- (void)setPrivateModalPresentationStyle:(UIModalPresentationStyle)modalPresentationStyle {
    NSNumber *styleNumber = [NSNumber numberWithInteger:modalPresentationStyle];
     objc_setAssociatedObject(self, @selector(privateModalPresentationStyle), styleNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIModalPresentationStyle)privateModalPresentationStyle {
    NSNumber *styleNumber = objc_getAssociatedObject(self, @selector(privateModalPresentationStyle));
    if (styleNumber == nil) {
        return NSNotFound;
    }
    return styleNumber.integerValue;
}

@end

最初,modalPresentationStyle的默认值是全屏,但在iOS 13中,它的变化是UIModalPresentationStyle.automatic。

如果你想要创建全屏视图控制器,你必须将modalPresentationStyle更改为fullScreen。

参考UIModalPresentationStyle苹果文档了解更多细节,并参考苹果人机界面指南了解应该在哪里使用哪种模式。

我添加的信息可能对某人有用。如果你有任何故事板segue,要回到旧的风格,你需要将kind属性设置为modal Present,并将Presentation属性设置为全屏。