Skip to content

SwiftUI组件-Button

普通按钮

swift
Button(action: {}, label: {
	Text("搜索")
		.font(.system(size: 16))
		.foregroundColor(.black)
		.opacity(0.48)
		.padding()
})
.border(Color.myPrimaryColor, width: 1)
Button(action: {}, label: {
	Text("搜索")
		.font(.system(size: 16))
		.foregroundColor(.black)
		.opacity(0.48)
		.padding()
})
.border(Color.myPrimaryColor, width: 1)

截屏2024-01-16 18.39.16.png

圆角按钮

swift
Button(action: {}, label: {
	Text("搜索")
		.font(.system(size: 16))
		.foregroundColor(.white)
		.padding()
})
.background(Color.red)
.clipShape(Capsule())
Button(action: {}, label: {
	Text("搜索")
		.font(.system(size: 16))
		.foregroundColor(.white)
		.padding()
})
.background(Color.red)
.clipShape(Capsule())

截屏2024-01-16 18.41.25.png

边框

swift
Button(action: {}, label: {
	Text("搜索")
		.font(.system(size: 16))
		.foregroundColor(.white)
		.padding()
})
.background(Color.red)
.border(Color.myPrimaryColor, width: 4)
Button(action: {}, label: {
	Text("搜索")
		.font(.system(size: 16))
		.foregroundColor(.white)
		.padding()
})
.background(Color.red)
.border(Color.myPrimaryColor, width: 4)

截屏2024-01-16 18.43.11.png

圆角按钮带边框

不能同时设置.border(Color.myPrimaryColor, width: 4).clipShape(Capsule()) 这样会产生奇怪的效果。

swift
Button(action: {}, label: {
	Text("搜索")
		.font(.system(size: 16))
		.foregroundColor(.black)
		.opacity(0.48)
		.padding()
})
.background(
	Capsule().stroke(Color.myPrimaryColor, lineWidth: 1)
)
Button(action: {}, label: {
	Text("搜索")
		.font(.system(size: 16))
		.foregroundColor(.black)
		.opacity(0.48)
		.padding()
})
.background(
	Capsule().stroke(Color.myPrimaryColor, lineWidth: 1)
)

截屏2024-01-16 18.46.42.png