首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >iOS26适配指南之UIButton

iOS26适配指南之UIButton

作者头像
YungFan
修改2025-08-18 10:15:21
修改2025-08-18 10:15:21
69700
代码可运行
举报
文章被收录于专栏:学海无涯学海无涯
运行总次数:0
代码可运行

介绍

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

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

适配 Liquid Glass

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

代码

代码语言:javascript
代码运行次数:0
运行
复制
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)
        }
    }
}

效果

按钮样式.png

symbolContentTransition

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

代码

代码语言:javascript
代码运行次数:0
运行
复制
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")
    }
}

效果

切换图标.gif
切换图标.gif
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 适配 Liquid Glass
    • 代码
    • 效果
  • symbolContentTransition
    • 代码
    • 效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档