iOS16適配指南之UISheetPresentationController

在 iOS 15 中 Apple 推出了 UISheetPresentationController,通過(guò)它可以控制 Modal 出來(lái)的 UIViewController 的顯示大小,且可以通過(guò)手勢(shì)在不同大小之間進(jìn)行切換。在 iOS 16 中,Modal 出來(lái)的 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()
        
        // 設(shè)置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
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容