You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI可展开列表(Expandable list)如何修改默认折叠箭头颜色?

SwiftUI 可展开列表箭头颜色修改方案

可以修改可展开列表右侧的默认蓝色箭头颜色,这个箭头是系统自带的披露指示器,默认继承全局 tint 颜色,有两种常用修改方案:


方案1:直接修改列表 tint(最简单)

只需要给List添加tint修饰符即可统一修改所有箭头的颜色,示例修改为红色的代码如下:

struct ContentView: View {
    let items: [Bookmark] = [.example1, .example2, .example3]

    var body: some View {
        List(items, children: \.items) { row in
            Image(systemName: row.icon)
            Text(row.name)
        }
        .tint(.red) // 这里修改箭头颜色,替换成你需要的任意颜色即可
        // 如果需要兼容iOS 15以下版本,替换为 .accentColor(.red)
    }
}

这个方案代码改动最小,劣势是会同时修改列表内其他默认控件的 tint 颜色。


方案2:自定义披露指示器(完全可控)

如果你不想影响列表内其他组件的 tint,或者需要自定义箭头的样式、大小,可以隐藏系统默认箭头,自己实现披露控件:

struct ContentView: View {
    let items: [Bookmark] = [.example1, .example2, .example3]

    var body: some View {
        List {
            ForEach(items) { item in
                // 自定义DisclosureGroup,替换系统默认生成的展开项
                DisclosureGroup {
                    // 递归渲染子项
                    if let subItems = item.items {
                        ForEach(subItems) { subItem in
                            HStack {
                                Image(systemName: subItem.icon)
                                Text(subItem.name)
                            }
                            .listRowInsets(EdgeInsets(top: 0, leading: 32, bottom: 0, trailing: 0))
                        }
                    }
                } label: {
                    HStack {
                        Image(systemName: item.icon)
                        Text(item.name)
                        Spacer()
                        // 自定义箭头,可自由修改颜色、大小、图标样式
                        Image(systemName: "chevron.right")
                            .foregroundColor(.green) // 自定义箭头颜色
                            .font(.system(size: 14, weight: .semibold))
                    }
                }
                // 隐藏系统默认的披露指示器
                .tint(.clear)
            }
        }
    }
}

这个方案可以完全自定义箭头的样式,灵活度最高。


内容的提问来源于stack exchange,提问作者hbr4u7vb2sljksdf2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 11:06:04