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?) {}
}
相关推荐
胖虎14 小时前
iOS 如何全局修改项目字体
ios·hook·ios字体·字体适配·ios字体适配
songgeb5 小时前
iOS App进入后台时会发生什么
ios
笑尘pyrotechnic6 小时前
运行,暂停,检查:探索如何使用LLDB进行有效调试
ios·objective-c·lldb
metaRTC7 小时前
webRTC IPC客户端React Native版编程指南
react native·react.js·ios·webrtc·p2p·ipc
ajassi200010 小时前
开源 Objective-C IOS 应用开发(十八)音频的播放
ios·开源·objective-c
2501_9159214310 小时前
Windows 系统下的 IPA 加密工具实战指南,如何在非 macOS 环境完成 IPA 混淆、加固与工程化处理
android·windows·macos·ios·小程序·uni-app·iphone
马拉萨的春天10 小时前
iOS的分类中为什么不能添加变量以及如何设置关联对象的弱引用效果
ios·分类·数据挖掘
ajassi200012 小时前
开源 Objective-C IOS 应用开发(十七)CAF音频的录制
ios·开源·objective-c
源码君miui520861 天前
JAVA国际版同城服务同城信息同城任务发布平台APP源码Android + IOS
android·java·ios
00后程序员张1 天前
接口调试从入门到精通,Fiddler抓包工具、代理配置与HTTPS抓包实战技巧
前端·ios·小程序·https·fiddler·uni-app·webview