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

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

相关推荐
zhyongrui7 小时前
SnipTrip 发热优化实战:从 60Hz 到 30Hz 的性能之旅
ios·swiftui·swift
大熊猫侯佩1 天前
赛博深渊(上):用 Apple Foundation Models 提炼“禁忌知识”的求生指南
llm·swiftui·大语言模型·foundationmodel·apple ai·apple 人工智能·summarize
zhyongrui2 天前
SwiftUI 光晕动画性能优化:消除托盘缩放卡顿的实战方案
ios·性能优化·swiftui
大熊猫侯佩3 天前
星际穿越:SwiftUI 如何让 ForEach 遍历异构数据(Heterogeneous)集合
swiftui·swift·遍历·foreach·any·异构集合·heterogeneous
符哥20083 天前
对比ArkTsUI和Flutter和 SwiftUI 和Jetpack Compose四个框架语法及使用场景。
flutter·ios·swiftui
大熊猫侯佩4 天前
越狱沙盒:SwiftUI fileImporter 的“数据偷渡”指南
swiftui·url·沙箱·sandbox·readfile·file importer·uniformtype
大熊猫侯佩9 天前
拯救巴别塔:WWDC24 全新 Translation API 实战
swiftui·wwdc·language·coreml·translation api·翻译接口·translationsess
初级代码游戏9 天前
iOS开发 SwiftUI 8:NavigationView 导航
ios·swiftui·swift
QWQ___qwq12 天前
1-s2.0-S0031320324008811-讲解
swiftui
Swift社区16 天前
使用 MetricKit 监控应用性能
ios·swiftui·swift