当前位置:网站首页>「SwiftUI」Button常见样式效果
「SwiftUI」Button常见样式效果
2022-08-07 05:12:00 【淡酒交魂】
iOS开发经常会使用Button组件
以下是三种常见的Button UI样式和相应的写法
样式一:背景颜色全部填充
样式如图:
相应代码:
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上套了一个圆角矩形形状的边框
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上方添加背景色,否则背景色的覆盖区域则是个矩形,无法展示圆角
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)
)
}
}
以上三种样式均为日常开发常用样式,以供大家参考选择。
边栏推荐
猜你喜欢

Linear Algebra Study Notes 4-1: Mathematical and Geometric Meaning of Linear Equations, Null Space/Solution Space/Kernel

Ansible - Explanation and Application of Playbook Playbook

The setting and clearing of the inconsistency between the data displayed in the Excel cell and the actual data

微信小程序安装vant组件库过程

Sigrity PowerDC仿真

后勤仓库物资领用发放小程序开发制作功能介绍

动态规划之背包问题

Redis 常用数据类型之 hash(哈希)

预约家教老师上门辅导小程序开发制作功能介绍

Network cable
随机推荐
聊聊Redis内存优化的7个神技
原生js实现图片列表懒加载和截流
Seq2Seq 粗浅理解
利用matlab工具箱的pid参数科学整定方法
洛谷P2456 二进制方程
谭浩强第五版第三章课后习题
CentOs通过yum在线安装Mysql
docker 中安装 MySQL 以及使用
编写一个程序将数组去除其中重复部分数据,最终得到一个升序且不重复的数组
[SemiDrive source code analysis] [MailBox inter-core communication] 48 - Modify the RPMSG IPCC RPC single transmission data size to 512 bytes (code actual combat part)
小 P 周刊 Vol.14
理想汽车×OceanBase:当造车新势力遇上数据库新势力
Terminal data encryption
2021-01-29
洛谷P1200 你的飞碟在这儿Your Ride Is Here
Linear algebra study notes 4-3: Solving homogeneous linear equations Ax=0, elimination method, row minimum echelon matrix RRFE
RT_thread的IIC设备学习笔记
POI操作Excel导入导出自定义工具类
ArrayList的扩容机制是如何实现的?
Redis 常用数据类型之 hash(哈希)