正如您在屏幕截图中看到的那样,按钮高度不会调整以适应文本大小,因此看起来很难看。我怎样才能增加按钮的高度,所以它看起来并不愚蠢。我的问题是,如何增加 SwiftUI 中按钮的高度?我正在尝试制作类似 Minecraft 的游戏的标题画面。
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
VStack (spacing: 8) {
Text("[Name not disclosed]Craft").font(.system(size: geometry.size.width/8))
Button(action: {
}) {
Text("Singleplayer").font(.system(size: geometry.size.width/20))
.frame(minWidth: geometry.size.width/2)
}
Button(action: {
}) {
Text("Multiplayer").font(.system(size: geometry.size.width/20))
.frame(minWidth: geometry.size.width/2)
}
HStack (spacing: 8) {
Button(action: {
}) {
Text("Options").font(.system(size: geometry.size.width/20))
.frame(minWidth: (geometry.size.width/4)-16)
}
Button(action: {
exit(EXIT_SUCCESS);
}) {
Text("Quit Game").font(.system(size: geometry.size.width/20))
.frame(minWidth: (geometry.size.width/4)-16)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 AppKit 中,我会通过将其等效键分配为 be?或将其单元格设为窗口的 default 来完成此操作。但是,这些在 SwiftUI 中似乎都不可能,那么如何将按钮设为默认窗口按钮?
好像应该很简单。
Button(action: {
}){
ZStack{
Circle()
.frame(width: 100, height: 100)
.foregroundColor(.blue)
Text("Press me")
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我:
我只能点击矩形部分。如果您能指出圆圈被切断的原因,也可以加分
编辑:原来这是 macOS 的问题。
编辑 2:正如 Asmari 在下面提到的,您可以使用 PlainButtonStyle:
var body: some View {
VStack{
Button(action: {
print("Pressed!")
}){
Text("Press me")
.frame(width: 100, height: 100)
.foregroundColor(Color.black)
.background(Color.red)
.clipShape(Circle())
}.buttonStyle(PlainButtonStyle())
}.frame(width: 300, height: 500)
}
}
Run Code Online (Sandbox Code Playgroud)
或使用自定义样式:
struct BlueButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.frame(width: 100, height: 100)
.foregroundColor(Color.black)
.background(Color.red)
.clipShape(Circle())
}
}
struct ContentView: View …Run Code Online (Sandbox Code Playgroud)