In Swiftui, How to Increase the Height of a Button

How to increase the size of a Button in SwiftUI? (Multiplatform project)

Here is a possible solution

For iOS

Sample Image

 Button(action: {
//add actions at here
}) {
VStack {
Text("Name")
}.frame(width: 100, height: 100)
.background(Color.yellow)
.cornerRadius(20)

}

For macOS (macOS version works well for iOS version too)

struct ContentView: View {

var body: some View {
VStack{


Button(action: {
//add actions at here
}) {
VStack {
Text("Button Name")
}.frame(width: 100, height: 100)
.background(Color.yellow)
.cornerRadius(20)

}.buttonStyle(CustomButtonStyle())
}
}
}

struct CustomButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.foregroundColor(Color.blue)
.cornerRadius(10.0)
.padding()
}
}

SwiftUI: how to size to fit a Button to expand to fill a VStack or HStack parent View?

Setting .infinity as the maxWidth, the frame(minWidth: maxWidth: minHeight:) API can be used to make a subview expand to fill:

VStack(alignment: .center, spacing: 20) {

NavigationLink(destination: CustomView()) {
Text("Button")
}.frame(minWidth: 100, maxWidth: .infinity, minHeight: 44)
.background(Color.primary)

Button(action: { self.isShowingAlert = true }) {
Text("Another Button")
}.frame(minWidth: 100, maxWidth: .infinity, minHeight: 44)
.background(Color.primary)

}.frame(width: 340)
.background(Color.secondary)

Sample Image

SwiftUI Button height on watchOS

Set the button’s buttonStyle(:) to PlainButtonStyle() and you’ll have full control over the padding and frame. The downside is that you no longer get the default button background, so you’ll need to recreate it if you need it.

SwiftUI Button Size/Shape/Color Not Changing

You have to use .buttonStyle(PlainButtonStyle()) to get rid of the default button UI.

Button(action: {
print("Delete button tapped!")
}) {
HStack {
Image(systemName: "trash")
.resizable()
.scaledToFit()
.imageScale(.large)
Text("Delete")
}
.padding()
.background(
Capsule().strokeBorder(Color.white, lineWidth: 1.25)
)
}
.accentColor(Color.white)
.buttonStyle(PlainButtonStyle())

Button is not Taking Given Height and Width in SwiftUI

You need to make image resizable, like

Image(ImgName)
.renderingMode(.original)
.resizable() // << here !!

also might want to add .aspectRatio(contentMode: .fit)



Related Topics



Leave a reply



Submit