iOS17适配指南之UITrait

UITrait与UITraitDefinition

  • iOS17 新增了一个协议UITraitDefinition,表示特征集合中特征的类型。通过遵守该协议可以实现自定义特征。
  • UITrait 则是UITraitDefinition.Type的别名。
swift 复制代码
@available(iOS 17.0, tvOS 17.0, *)
public protocol UITraitDefinition {
    associatedtype Value
    static var defaultValue: Self.Value { get }
    static var identifier: String { get }
    static var name: String { get }
    static var affectsColorAppearance: Bool { get }
}

@available(iOS 17.0, tvOS 17.0, *)
public typealias UITrait = UITraitDefinition.Type

UITraitCollection

  • UITraitCollection 包含的所有特征都遵守了UITraitDefinition协议。
  • 增加了新的构造方法。
  • 增加了修改方法。
swift 复制代码
// 创建
let customTraits = UITraitCollection { mutableTraits in
    mutableTraits.horizontalSizeClass = .compact
    mutableTraits.verticalSizeClass = .regular
    mutableTraits.userInterfaceStyle = .light
}

// 修改
let modifyTraits = customTraits.modifyingTraits { mutableTraits in
    mutableTraits.horizontalSizeClass = .regular
    mutableTraits.verticalSizeClass = .compact
    mutableTraits.userInterfaceStyle = .dark
}

traitCollectionDidChange()

UITraitEnvironment 协议中的traitCollectionDidChange()方法被废弃,监听特征改变需要使用UITraitChangeObservable协议中的相应的特征变化注册方法。

swift 复制代码
import UIKit

extension UIColor {
    static var viewBackgroundColor: UIColor {
        .init { (trait: UITraitCollection) -> UIColor in
            if trait.userInterfaceStyle == .dark {
                return .white
            }
            return .black
        }
    }

    static var viewControllerBackgroundColor: UIColor {
        .init { (trait: UITraitCollection) -> UIColor in
            if trait.userInterfaceStyle == .dark {
                return .red
            }
            return .green
        }
    }
}

class CustomView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)

        // iOS17之后
        registerForTraitChanges([UITraitUserInterfaceStyle.self], action: #selector(configureView))
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: iOS17之前,被废弃
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {}

    @objc private func configureView() {
        backgroundColor = .viewBackgroundColor
    }
}

class ViewController: UIViewController {
    lazy var customView: CustomView = {
        let customView = CustomView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        customView.center = view.center
        customView.backgroundColor = .viewBackgroundColor
        return customView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .viewControllerBackgroundColor
        view.addSubview(customView)
        // iOS17之后
        registerForTraitChanges([UITraitUserInterfaceStyle.self]) { (self: Self, previousTraitCollection: UITraitCollection) in
            self.view.backgroundColor = .viewControllerBackgroundColor
        }
    }

    // MARK: iOS17之前,被废弃
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {}
}
相关推荐
2501_915921432 小时前
查看iOS App实时日志的正确方式,多工具协同打造高效调试与问题定位体系(2025最新指南)
android·ios·小程序·https·uni-app·iphone·webview
ajassi20005 小时前
开源 Objective-C IOS 应用开发(四)Xcode工程文件结构
ios·开源·objective-c
G佳伟6 小时前
如何解决解决,微信小程序ios无法长按复制问题<text>设置 selectable=“true“不起作用
ios·微信小程序·小程序
Nick56836 小时前
Apple Pay 与 Google Pay 开发与结算全流程文档
ios·安卓·android-studio
全栈派森6 小时前
初见 Dart:这门新语言如何让你的 App「动」起来?
android·flutter·ios
HarderCoder7 小时前
Swift 内存管理:吃透 ARC 、weak、unowned
ios·swift
HarderCoder7 小时前
【Swift 访问控制全解析】一篇就够:从 open 到 private,让接口与实现各就其位
swift
Digitally8 小时前
5种将照片从iPhone传输到戴尔PC/笔记本电脑的方法
ios·电脑·iphone
ajassi20008 小时前
开源 Objective-C IOS 应用开发(三)第一个iPhone的APP
ios·开源·objective-c
HarderCoder8 小时前
Swift 6 实战:从“定时器轮询”到 AsyncSequence 的优雅实时推送
swift