# iOS13 UIModalPresentationStyle

![cover.png](https://img.hacpai.com/file/2019/11/cover-92130cce.png)

先声明一点，虽然这篇文章名字叫 UIModalPresentationStyle， 但是我们主要就了解 fullScreen， pageSheet 这两种 style。

> iOS13 之后，我们默认 `Present` 一个 view controller，我们会发现他是 pagesheet 的 style，也就是上边没有顶满的样子。

![WechatIMG932.jpeg](https://img.hacpai.com/file/2019/11/WechatIMG932-0ddf201f.jpeg)

## 一，开始

先了解一下 UIModalPresentationStyle 这个属性

```swift
public enum UIModalPresentationStyle : Int {

    case fullScreen // 全屏

    @available(iOS 3.2, *)
    case pageSheet  // 半屏

    @available(iOS 3.2, *)
    case formSheet

    @available(iOS 3.2, *)
    case currentContext

    @available(iOS 7.0, *)
    case custom

    @available(iOS 8.0, *)
    case overFullScreen

    @available(iOS 8.0, *)
    case overCurrentContext

    @available(iOS 8.0, *)
    case popover


    @available(iOS 7.0, *)
    case none

    @available(iOS 13.0, *)
    case automatic
}
```

我们主要关注这三个

1. fullScreen 全屏
2. pageSheet 半屏 开头图片展示的
3. automatic 系统去匹配

## 二，演示

> 这里会以 Demo 的形式，进行讲解。

我写了三种 present 的 style 方式，分别是 automatic，pagesheet，fullscreen。

| StyleName  |                                                                                                                             作用                                                                                                                            |
| ---------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| automatic  | 如果是 iOS13 操作系统，默认就是 PageSheet，如果是 iOS13 以下的版本就是 FullScreen，整个过程全由操作系统来决定，对于开发者来说，如果在 iOS13 的系统下 Debug 这个 ViewController 的值默认就是 PageSheet，**但是，如果有一些 CustomViewController 继承自 ViewController，自己重写了 Style 是 FullScreen，那么 present 出来就是 FullScreen 的 Style** |
| pagesheet  |                                                                                                                            半屏样式                                                                                                                           |
| fullscreen |                                                                                                                            全屏样式                                                                                                                           |

```swift
### ViewController.swift

import UIKit

class ViewController: UIViewController {

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

    override func viewDidAppear(_ animated: Bool) {

        // 没有设置nav的modalPresentationStyle属性，会用默认的 automatic
        present1()

        // 设置nav的modalPresentationStyles属性为pageSheet
        present2()

        // 设置nav的modalPresentationStyles属性为fullScreen
        present3()
    }

    func present1() {
        // 例子一:
        let vc = UIViewController()
        vc.view.backgroundColor = .red
        let nav = UINavigationController(rootViewController: vc)
        self.present(nav, animated: true, completion: nil)
    }

    func present2() {
        // 例子二:
        let vc = UIViewController()
        vc.view.backgroundColor = .red
        let nav = UINavigationController(rootViewController: vc)
        nav.modalPresentationStyle = .pageSheet
        self.present(nav, animated: true, completion: nil)
    }

    func present3() {
        // 例子三:
        let vc = UIViewController()
        vc.view.backgroundColor = .red
        let nav = UINavigationController(rootViewController: vc)
        nav.modalPresentationStyle = .fullScreen
        self.present(nav, animated: true, completion: nil)
    }

}
```

## 三，证据

> 如果还是没懂，没关系，我们来看看官方文档和国外程序员怎么解释的。

> 解释： 苹果文档说了，这个默认值是 automatic，然后网友疑问，我在 iOS13 上的 Controller 一直得到的都是 pageSheet，是任何页面都是 pageSheet 的格式么？
>
> 解释：

1. UIModalPresentationStyle.automatic 是在。pageSheet 和。fullscreen 之前进行选择的，它以来于被 present 出来的那个 controller 的状态
2. 如果你使用正常的 UIViewController，那么它默认就是。pageSheet 在 iOS13 系统下
3. 但是如果你使用了 UIViewController 的子类，例如像 UIImagePickerController，因为这个 Controller 继承自 UIViewController，所以根据业务需求重写了这个 style 的属性，改成 fullscreen 了，所以它看起来默认是 fullScreen
4. 如果你使用的是小于 iOS13 的版本，那么默认都是 fullscreen。

   ![present4.png](https://img.hacpai.com/file/2019/11/present4-f9ca33be.png)

> 来自官方文档，上面清楚的写到，Automatic 是默认的样式，对于绝大多数的 ViewController，例如 UIKit，系统会自动去匹配 pagesheet 这个 style 给这个 controller，但是在一些其他系统上面的 viewcontroller 它可能是不同的值。

![present3.jpeg](https://img.hacpai.com/file/2019/11/present3-a6cc60c5.jpeg)

> 如果你设置了 UIModalPresentationAutomatic 为自己的 style，那么，默认的弹出方式是由系统来选择的。

![present2.png](https://img.hacpai.com/file/2019/11/present2-651233b5.png)
