603

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

现在它默认不是全屏的,当我尝试向下滑动时,应用程序会自动关闭视图控制器。

如何防止这种行为并回到旧的全屏模式 vc?

模态行为

谢谢

4

26 回答 26

776

在 iOS 13 中,正如WWDC 2019 期间的平台国情所述,Apple 引入了新的默认卡片演示。为了强制全屏,您必须明确指定它:

let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)
于 2019-06-03T23:01:50.167 回答
244

我添加了一个可能对某人有用的信息。如果您有任何故事板转场,要回到旧样式,您需要将kind属性设置为Present Modally并将Presentation属性设置为Full Screen

在此处输入图像描述

于 2019-06-10T16:27:42.767 回答
120

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

fix_storyboard_presentation_default_behavior

于 2019-08-18T17:32:41.087 回答
115

有多种方法可以做到这一点,我认为每种方法都适合一个项目,但不适用于另一个项目,所以我想我会把它们留在这里,也许其他人会遇到不同的情况。

1-覆盖存在

如果你有一个BaseViewController你可以覆盖该present(_ viewControllerToPresent: animated flag: completion:)方法。

class BaseViewController: UIViewController {

  // ....

  override func present(_ viewControllerToPresent: UIViewController,
                        animated flag: Bool,
                        completion: (() -> Void)? = nil) {
    viewControllerToPresent.modalPresentationStyle = .fullScreen
    super.present(viewControllerToPresent, animated: flag, completion: completion)
  }

  // ....
}

使用这种方式,您不需要对任何present调用进行任何更改,因为我们只是覆盖了该present方法。

2- 扩展:

extension UIViewController {
  func presentInFullScreen(_ viewController: UIViewController,
                           animated: Bool,
                           completion: (() -> Void)? = nil) {
    viewController.modalPresentationStyle = .fullScreen
    present(viewController, animated: animated, completion: completion)
  }
}

用法:

presentInFullScreen(viewController, animated: true)

3- 对于一个 UIViewController

let viewController = UIViewController()
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: true, completion: nil)

4-来自故事板

选择一个 segue 并将演示文稿设置为FullScreen.
在此处输入图像描述

5- 调酒

extension UIViewController {

  static func swizzlePresent() {

    let orginalSelector = #selector(present(_: animated: completion:))
    let swizzledSelector = #selector(swizzledPresent)

    guard let orginalMethod = class_getInstanceMethod(self, orginalSelector), let swizzledMethod = class_getInstanceMethod(self, swizzledSelector) else{return}

    let didAddMethod = class_addMethod(self,
                                       orginalSelector,
                                       method_getImplementation(swizzledMethod),
                                       method_getTypeEncoding(swizzledMethod))

    if didAddMethod {
      class_replaceMethod(self,
                          swizzledSelector,
                          method_getImplementation(orginalMethod),
                          method_getTypeEncoding(orginalMethod))
    } else {
      method_exchangeImplementations(orginalMethod, swizzledMethod)
    }

  }

  @objc
  private func swizzledPresent(_ viewControllerToPresent: UIViewController,
                               animated flag: Bool,
                               completion: (() -> Void)? = nil) {
    if #available(iOS 13.0, *) {
      if viewControllerToPresent.modalPresentationStyle == .automatic {
        viewControllerToPresent.modalPresentationStyle = .fullScreen
      }
    }
    swizzledPresent(viewControllerToPresent, animated: flag, completion: completion)
   }
}

用法:
在你的AppDelegate里面application(_ application: didFinishLaunchingWithOptions)添加这一行:

UIViewController.swizzlePresent()

使用这种方式,您不需要对任何当前调用进行任何更改,因为我们正在运行时替换当前方法实现。
如果您需要知道什么是 swizzling,您可以查看此链接: https ://nshipster.com/swift-objc-runtime/

于 2019-10-06T08:13:10.160 回答
46

对于 Objective-C 用户

只需使用此代码

 [vc setModalPresentationStyle: UIModalPresentationFullScreen];

或者,如果您想在 iOS 13.0 中特别添加它,请使用

 if (@available(iOS 13.0, *)) {
     [vc setModalPresentationStyle: UIModalPresentationFullScreen];
 } else {
     // Fallback on earlier versions
 }
于 2019-09-24T11:40:34.530 回答
44

一个班轮:

modalPresentationStyle需要在正在呈现的navigationController 上进行设置。


iOS 13 及以下 iOS 版本 fullScreen with overCurrentContextand navigationController

测试代码

let controller = UIViewController()
let navigationController = UINavigationController(rootViewController: controller)
navigationController.modalPresentationStyle = .overCurrentContext
self.navigationController?.present(navigationController, animated: true, completion: nil)

modalPresentationStyle需要设置在navigationController

于 2019-11-27T07:21:28.990 回答
41

作为提示:如果您调用 present 到ViewController嵌入在 a 中的 aNavigationController您必须设置NavigationControllerto.fullScreen而不是 VC。

您可以像 @davidbates 一样执行此操作,也可以以编程方式执行此操作(例如 @pascalbros)。

这同样适用于UITabViewController

一个示例场景NavigationController

在此处输入图像描述

    //BaseNavigationController: UINavigationController {}
    let baseNavigationController = storyboard!.instantiateViewController(withIdentifier: "BaseNavigationController")
    var navigationController = UINavigationController(rootViewController: baseNavigationController)
    navigationController.modalPresentationStyle = .fullScreen
    navigationController.topViewController as? LoginViewController
    self.present(navigationViewController, animated: true, completion: nil)
于 2019-09-10T09:11:31.600 回答
41

我在 ios 13 上使用了 swizzling

import Foundation
import UIKit

private func _swizzling(forClass: AnyClass, originalSelector: Selector, swizzledSelector: Selector) {
    if let originalMethod = class_getInstanceMethod(forClass, originalSelector),
       let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) {
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }
}

extension UIViewController {

    static let preventPageSheetPresentation: Void = {
        if #available(iOS 13, *) {
            _swizzling(forClass: UIViewController.self,
                       originalSelector: #selector(present(_: animated: completion:)),
                       swizzledSelector: #selector(_swizzledPresent(_: animated: completion:)))
        }
    }()

    @available(iOS 13.0, *)
    @objc private func _swizzledPresent(_ viewControllerToPresent: UIViewController,
                                        animated flag: Bool,
                                        completion: (() -> Void)? = nil) {
        if viewControllerToPresent.modalPresentationStyle == .pageSheet
                   || viewControllerToPresent.modalPresentationStyle == .automatic {
            viewControllerToPresent.modalPresentationStyle = .fullScreen
        }
        _swizzledPresent(viewControllerToPresent, animated: flag, completion: completion)
    }
}

然后把这个

UIViewController.preventPageSheetPresentation

某处

例如在 AppDelegate

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {

    UIViewController.preventPageSheetPresentation
    // ...
    return true
}
于 2019-10-02T16:29:50.427 回答
29

这对我有用

let vc = self.storyboard?.instantiateViewController(withIdentifier: "storyboardID_cameraview1") as! CameraViewController
  
vc.modalPresentationStyle = .fullScreen
    
self.present(vc, animated: true, completion: nil)`
于 2020-11-01T03:34:40.633 回答
28

我需要做这两个:

  1. 将演示样式设置为全屏

    全屏

  2. 将顶部栏设置为半透明导航栏

顶栏

于 2019-11-27T09:54:41.987 回答
25

iOS 13 和 Swift 5.x 的最新版本

let vc = ViewController(nibName: "ViewController", bundle: nil)

vc.modalPresentationStyle = .fullScreen

self.present(vc, animated: true, completion: nil)
于 2020-04-06T15:47:30.487 回答
19

快速解决。上面已经有很好的答案了。我还添加了我的快速 2 点输入,显示在屏幕截图中。

  1. 如果您不使用Navigation Controller,则将Right Menu Inspector演示文稿设置为Full Screen

  2. 如果您正在使用Navigation Controller,那么默认情况下它将显示全屏,您无需执行任何操作。

在此处输入图像描述

于 2020-07-09T11:37:36.953 回答
13

这是一个简单的解决方案,无需编写一行代码。

  • 在 Storyboard 中选择视图控制器
  • 选择属性检查器
  • 根据下图将演示文稿“自动”设置为“全屏”

此更改使 iPad 应用程序的行为符合预期,否则新屏幕将作为弹出窗口显示在屏幕中央。

在此处输入图像描述

于 2019-10-19T11:29:56.427 回答
13

这是Objective-C的解决方案

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"ViewController"];

vc.modalPresentationStyle = UIModalPresentationFullScreen;

[self presentViewController:vc animated:YES completion:nil];
于 2019-11-13T07:04:38.393 回答
12

如果您有一个带有嵌入导航控制器的屏幕的 UITabController,您必须将 UITabController演示设置为 FullScreen,如下图所示

在此处输入图像描述

于 2019-09-23T17:38:48.623 回答
9

这是我在 ObjectiveC 中使用类别的修复版本。使用这种方法,您将拥有默认的 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
于 2019-12-03T17:24:30.800 回答
8

所有其他答案都足够了,但是对于像我们这样的大型项目以及在代码和情节提要中进行导航的大型项目,这是一项艰巨的任务。

在此处输入图像描述

对于那些积极使用 Storyboard 的人。这是我的建议:使用正则表达式。

以下格式不适用于全屏页面:

<segue destination="Bof-iQ-svK" kind="presentation" identifier="importSystem" modalPresentationStyle="fullScreen" id="bfy-FP-mlc"/>

以下格式适用于全屏页面:

<segue destination="7DQ-Kj-yFD" kind="presentation" identifier="defaultLandingToSystemInfo" modalPresentationStyle="fullScreen" id="Mjn-t2-yxe"/>

以下与 VS CODE 兼容的正则表达式会将所有旧样式页面转换为新样式页面。如果您使用其他正则表达式引擎/文本编辑器,您可能需要转义特殊字符。

搜索正则表达式

<segue destination="(.*)"\s* kind="show" identifier="(.*)" id="(.*)"/>

替换正则表达式

<segue destination="$1" kind="presentation" identifier="$2" modalPresentationStyle="fullScreen" id="$3"/>
于 2019-10-25T15:01:04.633 回答
5

最初,默认值为fullscreenmodalPresentationStyle,但在iOS 13中,其更改为UIModalPresentationStyle.automatic.

如果要制作全屏视图控制器,则必须modalPresentationStylefullScreen.

有关更多详细信息,请参阅UIModalPresentationStyle 苹果文档,并参阅苹果人机界面指南以了解应在何处使用哪种模式。

于 2019-11-06T06:43:36.583 回答
3
let Obj = MtViewController()
Obj.modalPresentationStyle = .overFullScreen
self.present(Obj, animated: true, completion: nil)

// 如果你想禁用滑动关闭它,添加行

Obj.isModalInPresentation = true

查看Apple 文档以获取更多信息。

于 2019-09-25T10:33:03.783 回答
3

你可以很容易地这样做打开你的故事板作为源代码并搜索kind="presentation",在所有带有 kind=presentation 的 seague 标签中添加一个额外的属性modalPresentationStyle="fullScreen"

于 2019-11-26T07:54:48.737 回答
3

我通过使用方法 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()
于 2020-05-24T11:37:05.703 回答
3

设置navigationController.modalPresentationStyleto.fullScreen已经在这里重复了一千多次,但让我向您展示另一个导致UIViewController/UINavigationController未显示的阻止程序,fullscreen即使所有属性都已正确设置。

就我而言,罪魁祸首隐藏在这一行

navigationController?.presentationController?.delegate = self

显然,在设置时,UIAdaptivePresentationControllerDelegate您需要在可选委托方法中指定演示样式

    public func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        presentationStyle
    }
于 2021-03-26T06:45:17.773 回答
1

为 UIViewController 创建一个类别(比如 UIViewController+PresentationStyle)。将以下代码添加到其中。

 -(UIModalPresentationStyle)modalPresentationStyle{
     return UIModalPresentationStyleFullScreen;
}
于 2019-11-25T09:40:21.297 回答
0

另一种方法是在您的应用程序中拥有自己的基本视图控制器组件,并使用基本设置实现指定和必需的初始化程序,如下所示:

class MyBaseViewController: UIViewController {

//MARK: Initialisers

/// Alternative initializer which allows you to set the modal presentation syle
/// - Parameter modalStyle: the presentation style to be used
init(with modalStyle:UIModalPresentationStyle) {
    super.init(nibName: nil, bundle: nil)
    self.setup(modalStyle: modalStyle)
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    // default modal presentation style as fullscreen
    self.setup(modalStyle: .fullScreen)
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    // default modal presentation style as fullscreen
    self.setup(modalStyle: .fullScreen)
}

//MARK: Private

/// Setup the view
///
/// - Parameter modalStyle: indicates which modal presentation style to be used
/// - Parameter modalPresentation: default true, it prevent modally presented view to be dismissible with the default swipe gesture
private func setup(modalStyle:UIModalPresentationStyle, modalPresentation:Bool = true){
    if #available(iOS 13, *) {
        self.modalPresentationStyle = modalStyle
        self.isModalInPresentation = modalPresentation
    }
}

注意:如果您的视图控制器包含在实际以模态方式呈现的导航控制器中,则导航控制器应该以相同的方式解决问题(意味着以相同的方式定制您的自定义导航控制器组件

在 iOS 13.1 和 iOS 12.4 上的 Xcode 11.1 上测试

希望能帮助到你

于 2019-10-09T07:42:51.087 回答
-1

上面的答案和建议是正确的,下面是另一个版本,并且以编程方式使用的有效方式。

#1 创建了一个 UIView 扩展

#2 创建了一个方法 ()

//#1
extension UIViewController {

//#2
func presentLocal(_ viewControllerToPresent: UIViewController, animated flag: 
Bool, completion: (() -> Void)? = nil) {

//Reusing below 2 lines :-)
viewControllerToPresent.modalPresentationStyle = .overCurrentContext
self.present(viewControllerToPresent, animated: flag, completion: completion)

  }
}

调用如下

let vc = MyViewController()
let nc = UINavigationController(rootViewController: vc)
sourceView.presentLocal(nc, animated: true, completion: nil)

或者

let vc = MyViewController()
sourceView.presentLocal(vc, animated: true, completion: nil)
于 2019-10-05T11:22:25.787 回答
-1
class MyViewController: UIViewController {

    convenience init() {
        self.init(nibName:nil, bundle:nil)
        self.modalPresentationStyle = .fullScreen
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

您可以继承 UIViewController 并在任何地方使用,而不是调用self.modalPresentationStyle = .fullScreen每个视图控制器MyViewController

于 2019-10-18T06:04:17.947 回答