我正在尝试SwiftUI。我不知道如何获取带有borderWidth的圆形按钮。下面是我尝试过的代码。
这是我的代码
struct ArrowButton : View {
var body: some View {
return Button(action: {}) {
HStack {
Text("SEE ALL")
.font(.headline)
.color(Color.init(red: 0.627, green: 0.776, blue: 0.965))
Image(systemName: "arrow.right")
.font(Font.title.weight(.bold))
}
}
.padding(.all, 9.0)
.border(Color.init(red: 0.627, green: 0.776, blue: 0.965))
.cornerRadius(5.0)
}
}
我应该使用哪些修饰符
答案 0 :(得分:2)
指定边框的宽度和角半径:
.border(Color.init(red: 0.627, green: 0.776, blue: 0.965),
width: 6.0,
cornerRadius: 12.0))
例如此代码:
struct ContentView : View {
var body: some View {
return Button(action: {}) {
HStack {
Text("SEE ALL")
.font(.headline)
.color(Color.init(red: 0.627, green: 0.776, blue: 0.965))
Image(systemName: "arrow.right")
.font(Font.title.weight(.bold))
}
}
.padding(.all, 20.0)
.border(Color.init(red: 0.627, green: 0.776, blue: 0.965),
width: 6.0,
cornerRadius: 40.0)
}
}
此视图的结果:
答案 1 :(得分:2)
View.border(:width:cornerRadius)
方法在最终的Xcode 11版本中不可用,但是您可以通过应用cornerRadius
和overlay
修饰符来达到相同的结果:
Button(action: {}) {
VStack {
Image(systemName: "star.fill")
Text("Hello world!")
}
.padding()
.accentColor(Color(.systemRed))
.background(Color(UIColor.systemRed.withAlphaComponent(0.4)))
.cornerRadius(4.0)
.overlay(
RoundedRectangle(cornerRadius: 4).stroke(Color(.systemRed), lineWidth: 2)
)
}
结果:
答案 2 :(得分:1)
这些示例都不适用于具有深色和白色背景颜色的按钮,并且它们都没有按状态更新,因此我建立了这个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)
}
}