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

效果

相关推荐
我是谁的程序员15 小时前
iOS 性能分析在 Windows 上怎么做,用 KXBuild 的 kxdevice 替代 Instruments
后端·ios
开心就好202515 小时前
通配符 Bundle ID 和精确 Bundle ID 怎么选,推送、iCloud 能不能用通配符
后端·ios
布莱克60520 小时前
Swift 枚举详解:从基础语法到关联值与模式匹配
开发语言·ios·swift·枚举
茶底世界之下20 小时前
冬日窗边的早餐:用 Awaking 留住冷光里的暖意
ios·app·产品
布莱克60520 小时前
Swift 闭包详解:从语法到实战的完整指南
服务器·开发语言·ios·ssh·swift·闭包
茶底世界之下2 天前
雨后的黄电车:用 Awaking 给灰色街景定一个主角
ios·app·产品
恋猫de小郭2 天前
iOS for Web ,把原生 iOS App 跑到进浏览器里面,倒反天罡
android·前端·ios
wuxianda10302 天前
苹果 App 上架 4.3(a) 问题解决方案(适用 uniapp、Objective-C、Swift、Flutter 等项目)
uni-app·objective-c·swift
wuxianda10302 天前
Swift 项目用 Xcode 脚手架起步:默认工程结构和依赖组合为什么会被识别成同源
开发语言·xcode·swift·4.3a·appstore审核
00后程序员3 天前
iOS 实时活动怎么开发,Windows 上用 KXBuild 和 ActivityKit 写灵动岛
后端·ios