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)
    }
}

效果

相关推荐
徐同保1 天前
通过ip访问nginx的服务时,被第一个server重定向了,通过设置default_server解决这个问题
ios·iphone
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 天前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview
夏幻灵2 天前
HTTPS全面解析:原理、加密机制与证书体
ios·iphone
TheNextByte12 天前
如何在iPhone上恢复已删除的笔记的综合指南
笔记·ios·iphone
rose and war2 天前
python和jinja版本问题导致的访问报500
python·ios
fendoudexiaoniao_ios2 天前
iOS 列表拖拽cell排序
ios·swift
2501_915106322 天前
当 Perfdog 开始收费之后,我重新整理了一替代方案
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
中小团队发布,跨平台 iOS 上架,证书、描述文件创建管理,测试分发一体化方案
android·ios·小程序·https·uni-app·iphone·webview
家里有只小肥猫2 天前
uniApp打包ios报错
ios·uni-app