不要将cornerRadius设置为按钮,而是对内部视图使用覆盖层:
编辑:如果您对按钮有背景,则还需要将cornerRadius应用于背景。
Button(action: {
print("sign up bin tapped")
}) {
Text("SIGN UP")
.frame(minWidth: 0, maxWidth: .infinity)
.font(.system(size: 18))
.padding()
.foregroundColor(.white)
.overlay(
RoundedRectangle(cornerRadius: 25)
.stroke(Color.white, lineWidth: 2)
)
}
.background(Color.yellow) // If you have this
.cornerRadius(25) // You also need the cornerRadius here
对于具有深色和白色背景颜色的按钮以及没有按下状态更新的任何示例都不起作用,因此我构建了LargeButton
视图,您可以在下面看到。希望这有所帮助,使用起来应该非常简单!
// White button with green border.
LargeButton(title: "Invite a Friend",
backgroundColor: Color.white,
foregroundColor: Color.green) {
print("Hello World")
}
// Yellow button without a border
LargeButton(title: "Invite a Friend",
backgroundColor: Color.yellow) {
print("Hello World")
}
struct LargeButtonStyle: ButtonStyle {
let backgroundColor: Color
let foregroundColor: Color
let isDisabled: Bool
func makeBody(configuration: Self.Configuration) -> some View {
let currentForegroundColor = isDisabled || configuration.isPressed ? foregroundColor.opacity(0.3) : foregroundColor
return configuration.label
.padding()
.foregroundColor(currentForegroundColor)
.background(isDisabled || configuration.isPressed ? backgroundColor.opacity(0.3) : backgroundColor)
// This is the key part, we are using both an overlay as well as cornerRadius
.cornerRadius(6)
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(currentForegroundColor, lineWidth: 1)
)
.padding([.top, .bottom], 10)
.font(Font.system(size: 19, weight: .semibold))
}
}
struct LargeButton: View {
private static let buttonHorizontalMargins: CGFloat = 20
var backgroundColor: Color
var foregroundColor: Color
private let title: String
private let action: () -> Void
// It would be nice to make this into a binding.
private let disabled: Bool
init(title: String,
disabled: Bool = false,
backgroundColor: Color = Color.green,
foregroundColor: Color = Color.white,
action: @escaping () -> Void) {
self.backgroundColor = backgroundColor
self.foregroundColor = foregroundColor
self.title = title
self.action = action
self.disabled = disabled
}
var body: some View {
HStack {
Spacer(minLength: LargeButton.buttonHorizontalMargins)
Button(action:self.action) {
Text(self.title)
.frame(maxWidth:.infinity)
}
.buttonStyle(LargeButtonStyle(backgroundColor: backgroundColor,
foregroundColor: foregroundColor,
isDisabled: disabled))
.disabled(self.disabled)
Spacer(minLength: LargeButton.buttonHorizontalMargins)
}
.frame(maxWidth:.infinity)
}
}
.cornerRadius(6)
和.overlay(RoundedRectangle(cornerRadius: 6))
效果很好!谢谢! - Honghao Z.bordered
修饰符现在,Button
使用 .buttonStyle(.bordered)
修饰符内置边框样式支持。为了获得最佳的平台特定样式,建议使用苹果提供的这些按钮的圆角半径。我们可以使用 .tint
修饰符将颜色更改为与系统风格一致的按钮样式,并调整背景和文本颜色:
Button("Add") { ... }
.buttonStyle(.bordered)
.tint(.green)
您可以使用.borderedProminent
让色调更加明显(加粗),并使用.controlSize
控制大小:
Button("food") { ... }
.tint(.red)
.controlSize(.small) // .large, .medium or .small
.buttonStyle(.borderedProminent)
您还可以在包含Button
的父级View
上使用此修饰符,并在子级Button
中使用.accentColor
切换较浅的颜色方案:
ScrollView {
LazyVStack {
Button("Test Button 1") { ... }
.buttonStyle(.borderedProminent)
.keyboardShortcut(.defaultAction) // Tapping `Return` key actions this button
Button("Test Button 2") { ... }
.tint(.accentColor)
}
}
.buttonStyle(.bordered)
.controlSize(.large)
由于某些原因,苹果不喜欢单线边框的按钮,这就是为什么在Xcode 12中.border()
修饰符被弃用的原因。随着这一改变,我建议开发者避免创建单线边框的按钮,因为它们现在不再是苹果人机界面指南中的首选。在所有地方使用凸起按钮也违反了HIG。
额外说明:苹果的.bordered
样式提供了跨设备类型的标准平台样式。此外,Button
会动态响应暗模式,并与动态类型(本机无障碍支持)一起缩放其大小。
.controlProminence(.increased)
已被弃用,推荐使用.buttonStyle(.borderedProminent)
。https://www.fivestars.blog/articles/xcode-13-beta-4/ - Felix只需要添加cornerRadius
参数:
.border(Color.white, width: 2, cornerRadius: 25)
使用这个简单的扩展程序:
extension View {
func border(_ color: Color, width: CGFloat, cornerRadius: CGFloat) -> some View {
overlay(RoundedRectangle(cornerRadius: cornerRadius).stroke(color, lineWidth: width))
}
}
extension
吗?@trinity420 - Mojtaba HosseiniSwift 5 & iOS 14 - 当按下时,边框也会有反应
struct PrimaryButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding(5)
.foregroundColor(configuration.isPressed ? Color.red.opacity(0.5) : .red)
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(configuration.isPressed ? Color.red.opacity(0.5) : .red, lineWidth: 1.5)
)
}
}
如何使用
Button("Hide") {
print("tapped")
}.buttonStyle(PrimaryButtonStyle())
Button(action: {
//define action
}) {
Image(systemName: "arrow.triangle.2.circlepath.circle.fill")
.imageScale(.large)
Text("Restart")
.font(.system(.title2))
}
.buttonStyle(.borderedProminent)
.buttonBorderShape(.capsule)
.controlSize(.large)
.buttonBorderShape(.roundedRectangle) //change bordershape see below
.buttonBorderShape(.roundedRectangle(radius: 4)) // see below
同样地,您可以更改按钮样式和控件大小。Xcode 11.4.1
Button(action: self.action) {
Text("Button Name")
.font(.system(size: 15))
.fontWeight(.bold)
.foregroundColor(.white)
.padding(10)
.background(Color.darkGray)
.cornerRadius(10)
}
.buttonStyle(PlainButtonStyle())
不需要添加叠加层。您可以使用框架修饰符替换填充修饰符。动作是在body变量之外的非返回方法。
特别针对 @MinonWeerasinghe:
Button(action: self.action) {
Text("Button Name")
.font(.system(size: 15))
.fontWeight(.bold)
.foregroundColor(.black)
.padding(10)
.background(RoundedRectangle(cornerRadius: 10).stroke().foregroundColor(Color.red))
.cornerRadius(10)
}
.buttonStyle(PlainButtonStyle())
var body: some View {
ZStack {
Color.green
.edgesIgnoringSafeArea(.all)
HStack {
Button(action: {
print("sign up bin tapped")
}){
HStack {
Text("SIGN UP")
.font(.system(size: 18))
}
.frame(minWidth: 0, maxWidth: 300)
.padding()
.foregroundColor(.white)
.overlay(
RoundedRectangle(cornerRadius: 40)
.stroke(Color.white, lineWidth: 2)
)
}
}
}
}
fontWeight
- iOS 16^ - North FaceSwiftUI:- 你也可以尝试这个!
目前我在同一个按钮上添加了不同形状的检查。如果你想要,你可以将我的代码分开。
// Global declaration
@State var isFollowed = false
Button {
// Toggle Your isFollowed with true and false
isFollowed = true
} label: {
Text(isFollowed ? "Following" : "Follow")
.padding(.horizontal, 10)
.padding(.vertical, 6)
.foregroundColor(.white)
.font(.custom(MyFont.GilroyBold, size: 13))
.background(
ZStack {
RoundedRectangle(
cornerRadius: 20,
style: .continuous
)
.fill(isFollowed ? .red : .clear)
RoundedRectangle(
cornerRadius: 20,
style: .continuous
)
.stroke(.red, lineWidth: 2)
}
)
}
例如,我需要“关注”和“正在关注”按钮。 在“关注”按钮上,我显示圆角和仅边框。 在“正在关注”按钮上,我显示圆角和背景颜色。