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?) {}
}
相关推荐
我唔知啊2 小时前
SwiftUI 无限循环轮播图 支持手动控制
ios·swiftui
QuantumLeap丶5 小时前
《Flutter全栈开发实战指南:从零到高级》- 08 -导航与路由管理
flutter·ios·dart
LinkTime_Cloud7 小时前
苹果牵手SpaceX,iPhone 18 Pro将实现卫星直接上网
ios·iphone
2501_915921437 小时前
iOS 26 描述文件管理与开发环境配置 多工具协作的实战指南
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915909067 小时前
iOS 抓包实战 从原理到复现、定位与真机取证全流程
android·ios·小程序·https·uni-app·iphone·webview
2501_915106327 小时前
HBuilder 上架 iOS 应用全流程指南:从云打包到开心上架(Appuploader)上传的跨平台发布实践
android·ios·小程序·https·uni-app·iphone·webview
2501_916007479 小时前
免费iOS加固方案指南
android·macos·ios·小程序·uni-app·cocoa·iphone
Zender Han18 小时前
Flutter 状态管理详解:深入理解与使用 Bloc
android·flutter·ios
00后程序员张1 天前
iOS 26 开发者工具推荐,构建高效调试与性能优化工作流
android·ios·性能优化·小程序·uni-app·iphone·webview
小范馆1 天前
通过 useEventBus 和 useEventCallBack 实现与原生 Android、鸿蒙、iOS 的事件交互
android·ios·harmonyos