iOS26适配指南之UIColor

介绍

随着 iOS 26 的发布,Apple 为开发者带来了对 HDR(高动态范围)颜色的原生支持。本文将带你快速了解 UIColor 在 iOS 26 中的新特性,并结合实际代码示例,介绍如何在项目中适配和使用这些功能。

UIColor新增HDR颜色支持

在 iOS 26 之前,UIColor 只能表示 SDR(标准动态范围)颜色。如今,系统新增了曝光值(Exposure / Linear Exposure)的支持,可以更好地展现 HDR 效果。

swift 复制代码
import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // iOS26新增
        let hdrColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0, exposure: 2.5)
        // let hdrColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0, linearExposure: 2.5)
        view.backgroundColor = hdrColor
    }
}

UIColorWell新增HDR支持

UIColorWell 是系统提供的颜色选择控件。在 iOS 26 中,它同样支持 HDR 颜色。

代码

swift 复制代码
import UIKit

class ViewController: UIViewController {
    lazy var colorWell: UIColorWell = {
        let colorWell = UIColorWell()
        colorWell.title = "设置背景色"
        // iOS26新增
        colorWell.maximumLinearExposure = 2.0
        // iOS26新增,是否支持吸取颜色
        colorWell.supportsEyedropper = false
        colorWell.addTarget(self, action: #selector(valueChanged), for: .valueChanged)
        colorWell.sizeToFit()
        colorWell.center = view.center
        return colorWell
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(colorWell)
    }

    @objc func valueChanged(_ sender: UIColor) {
        view.backgroundColor = colorWell.selectedColor
    }
}

效果

UIColorPickerViewController新增HDR支持

UIColorPickerViewController 在 iOS 26 中也升级了,支持 HDR 颜色选择,新增属性与 UIColorWell 一致。

代码

swift 复制代码
import UIKit

class ViewController: UIViewController {
    lazy var colorPickerViewController: UIColorPickerViewController = {
        var colorPickerViewController = UIColorPickerViewController()
        colorPickerViewController.title = "颜色选择器"
        colorPickerViewController.delegate = self
        // iOS26新增
        colorPickerViewController.maximumLinearExposure = 2.0
        // iOS26新增
        colorPickerViewController.supportsEyedropper = false
        return colorPickerViewController
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        present(colorPickerViewController, animated: true, completion: nil)
    }
}

extension ViewController: UIColorPickerViewControllerDelegate {
    func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
        let color = viewController.selectedColor
        view.backgroundColor = color
    }
    
    func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
        dismiss(animated: true, completion: nil)
    }
}

效果

相关推荐
未来侦察班2 小时前
一晃13年过去了,苹果的Airdrop依然很坚挺。
macos·ios·苹果vision pro
锐意无限8 小时前
Swift 扩展归纳--- UIView
开发语言·ios·swift
符哥20088 小时前
用Apollo + RxSwift + RxCocoa搭建一套网络请求框架
网络·ios·rxswift
文件夹__iOS12 小时前
AsyncStream 进阶实战:SwiftUI 全局消息流极简实现
ios·swiftui·swift
2501_9160088915 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
忆江南15 小时前
Flutter深度全解析
ios
山水域15 小时前
Swift 6 严格并发检查:@Sendable 与 Actor 隔离的深度解析
ios
楚轩努力变强16 小时前
iOS 自动化环境配置指南 (Appium + WebDriverAgent)
javascript·学习·macos·ios·appium·自动化
游戏开发爱好者82 天前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
黑码哥2 天前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder