How to increase the size of a Button in SwiftUI? (Multiplatform project)
Here is a possible solution
For iOS
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)
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
Share Data Between Main App and Widget in Swiftui For iOS 14
String Value to Unsafepointer≪Uint8≫ Function Parameter Behavior
Xcode 8 Beta 3 Use Legacy Swift Issue
Forced to Cast, Even If Protocol Requires Given Type
Why Are Emoji Characters Like 👩👩👧👦 Treated So Strangely in Swift Strings
How to Set Layer Cornerradius For Only Bottom-Left, Bottom-Right, and Top-Left Corner
Real Time Nstask Output to Nstextview With Swift
What Does It Mean That String and Character Comparisons in Swift Are Not Locale-Sensitive
Swift Dictionary Get Key For Value
Unexpected Non-Void Return Value in Void Function (Swift 2.0)
Send Data from Tableview to Detailview Swift
Load Local Web Files & Resources in Wkwebview
How to Determine If a String Contains a Character from a Set in Swift