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,用于设置view内容不可达时的占位内容。
增加了新的生命周期函数viewIsAppearing(),调用时机介于viewWillAppear()与viewDidAppear()之间,并且兼容到 iOS 13。

import UIKit

class ViewController: UIViewController {
    // UIContentUnavailableConfiguration
    lazy var emptyConfig: UIContentUnavailableConfiguration = {
        var config = UIContentUnavailableConfiguration.empty()
        config.text = "暂无数据"
        config.image = UIImage(systemName: "exclamationmark.triangle")
        return config
    }()

    // MARK: view完成内存加载
    override func viewDidLoad() {
        super.viewDidLoad()
        print(#function)
    }

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

    // MARK: view已经显示,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()
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容