【问题标题】:In SwiftUI, How do I increase the height of a button?在 SwiftUI 中,如何增加按钮的高度?
【发布时间】:2020-04-04 23:45:03
【问题描述】:

正如您在屏幕截图中看到的,按钮高度没有调整到适合文本大小,使它看起来很丑。我怎样才能增加按钮的高度,所以它看起来并不愚蠢。我的问题是,如何增加 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)
                }
            }
        }
    }
}

【问题讨论】:

标签: swift macos user-interface button swiftui


【解决方案1】:

请尝试以下代码:

Button(action: {
       //do action       
}) {
    Text("SIGN IN")
        .frame(width: 200 , height: 50, alignment: .center)
        //You need to change height & width as per your requirement
}
 .background(Color.blue)
 .foregroundColor(Color.white)
 .cornerRadius(5)

输出

【讨论】:

    【解决方案2】:

    如果您只需要 Button 的标题,请使用更简单的初始化程序:

    Button("Click me") {
        // Perform action here
    }
    .frame(width: 100, height: 100)
    .background(Color.yellow)
    

    请注意 frame 修饰符必须位于 background 之前以使其看起来更大。否则,你看不出区别。

    【讨论】:

    • 非常感谢,你拯救了我的一天,你的“注意”部分创造了奇迹!
    • 这种方法的问题是只有文本可以点击,整个黄色背景不是。
    【解决方案3】:

    你只需要设置PlainButtonStyle并按照你的意愿绘制...

    例如,这是您的一个按钮:

    Button(action: {
    
    }) {
        Text("Singleplayer").font(.system(size: geometry.size.width/20))
            .padding()
            .background(RoundedRectangle(cornerRadius: 8).fill(Color.blue))
            .frame(minWidth: geometry.size.width/2)
    }
    .buttonStyle(PlainButtonStyle())
    

    【讨论】:

    • 如上所述,框架修饰符应该在背景修饰符之前,否则蓝色不会填满整个框架
    【解决方案4】:

    你需要改变栈的高度

    struct ContentView: View {
    
       @State private var arr = ["String"]
    
        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)
                                }
                        } .frame(width: 100, height: 100, alignment: .leading) .background(Color.red)
                    }
                }
        }
    }
    

    【讨论】:

    • 这里的几何是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多