「SwiftUI」Button常见样式效果

iOS开发经常会使用Button组件

以下是三种常见的Button UI样式和相应的写法

样式一:背景颜色全部填充

样式如图:

相应代码:

swift 复制代码
struct ContentView: View {
    
    var body: some View {
        Button {
            print("open")
        } label: {
            HStack{
                Image("On")
                
                Text("开启")
                    .bold()
                    .font(Font.system(size: 22))
                    .frame(width:300, height: 80, alignment: .center)
            }
        }
        .foregroundColor(Color.white)
        .background(Color(.sRGB,red: 62/255.0, green: 173/255.0, blue: 247/255.0))
        .clipShape(RoundedRectangle(cornerRadius: 8))
    }
}

注意:frame属性写在Text下方才可以使点击范围为整个摁钮区域,否则仅文字区域

样式二:圆角矩形边框包围(无背景色)

样式如图:

相应代码:

相当于在Button上套了一个圆角矩形形状的边框

swift 复制代码
struct ContentView: View {
    var body: some View {
        Button {
            print("close")
        } label: {
            HStack{
                Image("Off")

                Text("关闭")
                    .bold()
                    .font(Font.system(size: 22))
                    .frame(width:300, height: 80, alignment: .center)
            }
        }
        .foregroundColor(Color(.sRGB,red: 62/255.0, green: 173/255.0, blue: 247/255.0))
        //Button边框
        .overlay(
        RoundedRectangle(cornerRadius: 10, style: .continuous)
        //边框线
        .stroke(Color(.sRGB,red: 62/255.0, green: 173/255.0, blue: 247/255.0), lineWidth: 2)
        )
    }
}

样式三:圆角矩形边框包围(带背景色)

样式如图:

相应代码:

需要添加Button圆角矩形的背景色,那需要添加一个cornerRadius的圆角角度,
并且在cornerRadius上方添加背景色,否则背景色的覆盖区域则是个矩形,无法展示圆角

swift 复制代码
struct ContentView: View {
    var body: some View {
        Button {
            print("close")
        } label: {
            HStack{
                Image("Off")

                Text("关闭")
                    .bold()
                    .font(Font.system(size: 22))
                    .frame(width:300, height: 80, alignment: .center)
            }
        }
        .foregroundColor(Color(.sRGB,red: 62/255.0, green: 173/255.0, blue: 247/255.0))
        //Button边框
        .background(Color.red.opacity(0.3))
        .cornerRadius(10)
        .overlay(
        RoundedRectangle(cornerRadius: 10, style: .continuous)
        .stroke(Color(.sRGB,red: 62/255.0, green: 173/255.0, blue: 247/255.0), lineWidth: 2)
        )
        
    }
}

以上三种样式均为日常开发常用样式,以供大家参考选择。

相关推荐
东坡肘子4 天前
举手之劳 -- 肘子的 Swift 周报 #151
人工智能·swiftui·swift
sakiko_5 天前
Swift学习笔记42-SwiftUI的属性包装器(讲解+面试)
笔记·学习·ios·swiftui·swift
Daniel_Coder7 天前
我用 SwiftUI + GRDB 做了一个「按时吃药·服药提醒」App
ios·swiftui·swift·widgetkit
懋学的前端攻城狮10 天前
把 SwiftUI List 换成 NSTableView:一次目录树卡顿的排查记录
macos·swiftui·swift
东坡肘子11 天前
AI 想放弃了,人没有 -- 肘子的 Swift 周报 #150
人工智能·swiftui·swift
东坡肘子18 天前
越来越慢的 App Review,拦住了谁? -- 肘子的 Swift 周报 #149
人工智能·swiftui·swift
大熊猫侯佩23 天前
Xcode 预览新升级:arguments 让界面调试井井有条
swiftui·xcode·swift
大熊猫侯佩24 天前
WWDC26:把 SwiftUI Toolbar 真正握在手心里
swiftui·swift·apple
大熊猫侯佩24 天前
WWDC 26 全新 ResultsObserver:终于能在 View 外面盯着 SwiftData 了
swiftui·swift·apple
东坡肘子25 天前
Apple Intelligence 已通过审核,即将在中国提供服务 -- 肘子的 Swift 周报 #148
人工智能·swiftui·swift