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

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

相关推荐
东坡肘子1 天前
Swift 并发正被更广泛地接纳 -- 肘子的 Swift 周报 #133
人工智能·swiftui·swift
文件夹__iOS4 天前
SwiftUI 核心选型:class + ObservableObject VS struct + @State
ios·swiftui·swift
Wenzar_5 天前
# 发散创新:SwiftUI 中状态管理的深度实践与重构艺术 在 SwiftUI 的世界里,**状态驱动 UI 是核心哲学**。但随
java·python·ui·重构·swiftui
大熊猫侯佩6 天前
GeometryReader 生存指南(下集):与恶魔共舞——陷阱、禁忌与最终救赎
swiftui·performance·layout·frame·stack·geometryreader·preferencekey
大熊猫侯佩7 天前
别被系统绑架:SwiftUI List 替换背后的底层逻辑
swiftui·swift·apple
东坡肘子8 天前
从 OpenSwiftUI 到 DanceUI:换个方式 Dive SwiftUI -- 肘子的 Swift 周报 #132
人工智能·swiftui·swift
用户79457223954139 天前
【SwiftyJSON】拯救你的 as? [String: Any]——链式 JSON 访问的正确姿势
swiftui·objective-c·swift
用户79457223954139 天前
【Moya】为什么你的 Alamofire 代码需要再封装一层?
swiftui·objective-c·swift
空中海10 天前
第二章:SwiftUI 视图基础
ios·swiftui·swift