iOS16适配指南之UISheetPresentationCon
2022-07-14 本文已影响0人
YungFan
在 iOS 15 中 Apple 推出了 UISheetPresentationController,通过它可以控制 Modal 出来的 UIViewController 的显示大小,且可以通过手势在不同大小之间进行切换。在 iOS 16 中,Modal 出来的 UIViewController 可以自定义显示的大小。
// Created by YungFan
import UIKit
extension UISheetPresentationController.Detent.Identifier {
static let small = UISheetPresentationController.Detent.Identifier("small")
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let modalViewController = ModalViewController()
// 设置UISheetPresentationController
if let sheet = modalViewController.sheetPresentationController {
// 支持的自定义显示大小
sheet.detents = [
.custom { _ in
200.0 // 固定大小
},
.custom(identifier: .small) { context in
0.1 * context.maximumDetentValue // 占上下文最大尺寸的0.1
},
.custom { context in //
0.5 * context.maximumDetentValue // 占上下文最大尺寸的0.5
},
.large()]
sheet.prefersGrabberVisible = true
}
present(modalViewController, animated: true)
}
}
class ModalViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
}
}