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

效果

相关推荐
kkoral13 小时前
基于MS-Swift 为 Qwen3-0.6B-Base 模型搭建可直接调用的 API 服务
python·conda·fastapi·swift
QuantumLeap丶1 天前
《Flutter全栈开发实战指南:从零到高级》- 26 -持续集成与部署
android·flutter·ios
2501_915918411 天前
TCP 抓包分析在复杂网络问题中的作用,从连接和数据流层面理解系统异常行为
网络·网络协议·tcp/ip·ios·小程序·uni-app·iphone
二流小码农1 天前
鸿蒙开发:个人开发者如何使用华为账号登录
android·ios·harmonyos
Yorelee.1 天前
ms-swift在训练时遇到的部分问题及解决方案
开发语言·nlp·transformer·swift
wvy1 天前
Xcode 26还没有适配SceneDelegate的app建议尽早适配
ios
游戏开发爱好者81 天前
苹果 App 上架流程,结合 Xcode、CI 等常见工具
macos·ios·ci/cd·小程序·uni-app·iphone·xcode
前端老白2 天前
webview在微信小程序中,安卓加载失败,IOS正常加载
android·ios·微信小程序·webview
2501_915106322 天前
用 HBuilder 上架 iOS 应用时如何管理Bundle ID、证书与描述文件
android·ios·小程序·https·uni-app·iphone·webview
2501_915909062 天前
资源文件混淆在 iOS 应用安全中的实际价值
android·安全·ios·小程序·uni-app·iphone·webview