In swiftui, How do I increase the height of a button?

三世轮回 提交于 2020-01-21 06:51:11

问题


As you can see in the screenshot, the button height does not adjust to fit the text size, making it look ugly. How can I increase the hight of the buttons, so it does not look stupid. My question is, how do I increase the height of buttons in swiftui. I am trying to make the titlescreen of my minecraft like game.

    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)
                    }
                }
            }
        }
    }
}


回答1:


You just need to set PlainButtonStyle and draw it as you wish...

Here is for example one of your button:

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())




回答2:


You need to change height of the stack

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)
                }
            }
    }
}


来源:https://stackoverflow.com/questions/59290097/in-swiftui-how-do-i-increase-the-height-of-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!