iOS26适配指南之UIButton

介绍

在 iOS 26 中,UIButton 迎来了两项非常实用的更新:

  • Liquid Glass 风格配置方法 --- 让按钮拥有全新的半透明折射质感,完美融入 iOS 26 的视觉系统。
  • Symbol 动画切换 --- 通过 symbolContentTransition 实现更顺滑的 SF Symbols 切换效果。

适配 Liquid Glass

UIButton.Configuration 增加了符合 Liquid Glass 风格的配置方法:glass()clearGlass()prominentGlass()prominentClearGlass()

代码

swift 复制代码
import UIKit

class ViewController: UIViewController {
    let configs: [UIButton.Configuration] = {
        [
            {
                // iOS26新增
                var config = UIButton.Configuration.glass()
                config.title = "喜欢"
                config.image = UIImage(systemName: "heart")
                return config
            }(),
            {
                // iOS26新增
                var config = UIButton.Configuration.clearGlass()
                config.title = "收藏"
                config.image = UIImage(systemName: "star")
                return config
            }(),
            {
                // iOS26新增
                var config = UIButton.Configuration.prominentGlass()
                config.title = "分享"
                config.image = UIImage(systemName: "square.and.arrow.up")
                return config
            }(),
            {
                // iOS26新增
                var config = UIButton.Configuration.prominentClearGlass()
                config.title = "下载"
                config.image = UIImage(systemName: "arrow.down.circle")
                return config
            }()
        ]
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .systemGray

        for (index, config) in configs.enumerated() {
            let button = UIButton(frame: CGRect(x: 120, y: 220 + CGFloat(index) * 70, width: 160, height: 60))
            button.configuration = config
            view.addSubview(button)
        }
    }
}

效果

symbolContentTransition

UIButton.Configuration 增加了类型UISymbolContentTransition?的属性symbolContentTransition,用于切换 SF Symbols 图标,并且可以呈现切换动画。

代码

swift 复制代码
import UIKit

class ViewController: UIViewController {
    let button = UIButton(frame: CGRect(x: 100, y: 200, width: 200, height: 200))

    override func viewDidLoad() {
        super.viewDidLoad()

        var config = UIButton.Configuration.plain()
        config.image = UIImage(systemName: "heart.circle")
        config.preferredSymbolConfigurationForImage = UIImage.SymbolConfiguration(pointSize: 50, weight: .thin)
        // iOS26新增
        config.symbolContentTransition = UISymbolContentTransition(.replace, options: .speed(0.1))
        button.configuration = config
        button.isSymbolAnimationEnabled = true
        button.tintColor = .systemRed
        view.addSubview(button)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        button.configuration?.image = UIImage(systemName: "heart.circle.fill")
    }
}

效果

相关推荐
2501_915106327 小时前
HTTPS 爬虫实战指南 从握手原理到反爬应对与流量抓包分析
爬虫·网络协议·ios·小程序·https·uni-app·iphone
2501_916007477 小时前
iOS 上架技术支持全流程解析,从签名配置到使用 开心上架 的实战经验分享
android·macos·ios·小程序·uni-app·cocoa·iphone
HarderCoder8 小时前
【Swift 筑基记】把“结构体”与“类”掰开揉碎——从值类型与引用类型说起
swift
HarderCoder8 小时前
Swift 字符串与字符完全导读(三):比较、正则、性能与跨平台实战
swift
HarderCoder8 小时前
Swift 字符串与字符完全导读(一):从字面量到 Unicode 的实战之旅
swift
HarderCoder9 小时前
Swift 字符串与字符完全导读(二):Unicode 视图、索引系统与内存陷阱
swift
非专业程序员Ping17 小时前
一文读懂字体文件
ios·swift·assembly·font
wahkim21 小时前
移动端开发工具集锦
flutter·ios·android studio·swift
2501_916007471 天前
提升 iOS 26 系统流畅度的实战指南,多工具组合监控
android·macos·ios·小程序·uni-app·cocoa·iphone
hellojackjiang20111 天前
全面适配iOS 26液态玻璃,基于开源IM即时通讯框架MobileIMSDK:RainbowChat-iOS端v10.2发布
ios·网络编程·即时通讯·im开发·rainbowchat