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

效果

相关推荐
songgeb3 天前
启发式 UI 自动化:从线性剧本到每步读屏决策
ios·测试
东坡肘子6 天前
Swift 还让你 Excited 吗?-- 肘子的 Swift 周报 #141
人工智能·swiftui·swift
壹方秘境6 天前
我用Go语言开发了一个跨平台的HTTPS抓包和调试工具
前端·后端·ios
sweet丶8 天前
Swift 元编程-Macro
swift
初级代码游戏12 天前
easy Photo Clean公测版:快速清理iPhone照片 邀请公测
ios·iphone
库奇噜啦呼12 天前
【iOS】RunLoop学习
学习·ios
影寂ldy12 天前
WinForm PictureBox控件 + ImageList组件 完整笔记
开发语言·笔记·swift
黑科技iOS上架12 天前
iOS应用周末提交什么情况算卡审
经验分享·ios
zzb158012 天前
ios基础-MVC-UIView
ios·mvc·cocoa