iOS 17新增的一些API

1、日夜間切換

// 該方法已被銷毀在17及以后
func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)

使用如下方法替換:

override func viewDidLoad() {
    super.viewDidLoad()

    // 二者擇其一即可
    if #available(iOS 17.0, *) {
        registerForTraitChanges(
            [UITraitUserInterfaceStyle.self],
            target: self,
            action: #selector(traitConfigureView)
        )

        registerForTraitChanges(
            [UITraitUserInterfaceStyle.self],
            handler: { (self: Self, _: UITraitCollection) in
                if self.traitCollection.userInterfaceStyle == .light {
                    print("App switched to light mode")
                } else {
                    print("App switched to dark mode")
                }

                print("=====registerForTraitChanges")
            }
        )
    }
}

@objc private func traitConfigureView() {
    print("=====registerForTraitChanges")
}

2、UIViewController

增加了新的屬性contentUnavailableConfiguration,用于設(shè)置view內(nèi)容不可達(dá)時(shí)的占位內(nèi)容。
增加了新的生命周期函數(shù)viewIsAppearing(),調(diào)用時(shí)機(jī)介于viewWillAppear()與viewDidAppear()之間,并且兼容到 iOS 13。

import UIKit

class ViewController: UIViewController {
    // UIContentUnavailableConfiguration
    lazy var emptyConfig: UIContentUnavailableConfiguration = {
        var config = UIContentUnavailableConfiguration.empty()
        config.text = "暫無(wú)數(shù)據(jù)"
        config.image = UIImage(systemName: "exclamationmark.triangle")
        return config
    }()

    // MARK: view完成內(nèi)存加載
    override func viewDidLoad() {
        super.viewDidLoad()
        print(#function)
    }

    // MARK: view即將顯示
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print(#function)
    }

    // MARK: view已經(jīng)顯示,iOS17新增
    override func viewIsAppearing(_ animated: Bool) {
        super.viewIsAppearing(animated)

        contentUnavailableConfiguration = emptyConfig
        // 切換UIContentUnavailableConfiguration
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            let loadingConfig = UIContentUnavailableConfiguration.loading()
            self.contentUnavailableConfiguration = loadingConfig
        }
        // 移除UIContentUnavailableConfiguration
        DispatchQueue.main.asyncAfter(deadline: .now() + 6) {
            self.contentUnavailableConfiguration = nil
            self.view.backgroundColor = .systemTeal
        }

        print(#function)
    }

    // MARK: view即將布局子UIView
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        print(#function)
    }

    // MARK: view完成布局子UIView
    override func viewDidLayoutSubviews() {
        super.viewWillLayoutSubviews()
        print(#function)
    }

    // MARK: view完全顯示
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print(#function)
    }

    // MARK: view即將消失
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        print(#function)
    }

    // MARK: view徹底消失
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print(#function)
    }
}

#Preview {
    ViewController()
}
?著作權(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)容