Ios SwiftUI中的按钮大小和颜色
我是SwiftUI和iOS开发的新手 我试图创建两个按钮,但无法更改背景颜色或大小 看起来是这样的: 代码如下:Ios SwiftUI中的按钮大小和颜色,ios,swift,swiftui,Ios,Swift,Swiftui,我是SwiftUI和iOS开发的新手 我试图创建两个按钮,但无法更改背景颜色或大小 看起来是这样的: 代码如下: HStack { Button( action: { print("click") }){ Text("Login") .foregroundColor(.purple)
HStack {
Button( action: {
print("click")
}){
Text("Login")
.foregroundColor(.purple)
.padding()
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.purple, lineWidth: 1)
)
}
Button( action: {
print("click")
}){
Text("Register")
.foregroundColor(.white)
.padding()
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.black, lineWidth: 1)
)
}
}
我希望得到一个按钮,这是几乎使用了一半的屏幕与边缘的10px的每一面。目标是让2个按钮几乎覆盖显示器的宽度。我也试着让它变薄。顶部和底部的边框之间的距离太大,我希望它靠近文本,而左侧和右侧的边框必须更宽
我也不知道如何把按钮的背景色改成黑色
有什么想法吗
谢谢您可以创建自己的
按钮样式
,您可以完全定制和重用:
我们能记住的最重要的事情是,当我们面临大小不确定的情况时,使用形状填充所有可用空间,然后根据需要限制它们,就像我在代码中所做的那样,您可以更改设计的数字
import SwiftUI
struct ContentView: View {
var body: some View {
HStack {
Button( action: {
print("Login Button")
}){
CustomButtonView(stringOfButton: "Login")
}
Button( action: {
print("Register Button")
}){
CustomButtonView(stringOfButton: "Register")
}
}
.padding()
}
}
=>长话短说 也许有人觉得它很方便
struct test: View {
let roundRect = RoundedRectangle(cornerRadius: 25.0)
var body: some View {
HStack {
Button( action: {
print("click")
}){
Text("Login")
.frame(width: UIScreen.main.bounds.width*0.4, height: 50, alignment: .center)
.background(roundRect.fill(Color.orange))
.overlay(roundRect.stroke())
}
}.foregroundColor(.purple)
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
HStack {
Button( action: {
print("Login Button")
}){
CustomButtonView(stringOfButton: "Login")
}
Button( action: {
print("Register Button")
}){
CustomButtonView(stringOfButton: "Register")
}
}
.padding()
}
}
struct CustomButtonView: View {
var stringOfButton: String
var body: some View {
ZStack {
Rectangle()
.fill(Color.black)
.cornerRadius(20)
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.purple, lineWidth: 1)
)
.frame(height: 50, alignment: .center)
Text(stringOfButton)
.foregroundColor(.purple)
}
}
}
struct test: View {
let roundRect = RoundedRectangle(cornerRadius: 25.0)
var body: some View {
HStack {
Button( action: {
print("click")
}){
Text("Login")
.frame(width: UIScreen.main.bounds.width*0.4, height: 50, alignment: .center)
.background(roundRect.fill(Color.orange))
.overlay(roundRect.stroke())
}
}.foregroundColor(.purple)
}
}