Benjamin Clanet
Benjamin Clanet

Reputation: 1116

SwiftUI: Have button's right and left stick to parent's right and left

SwiftUI question here...

I am trying to layout my button so it has the full width of the screen minus some padding of 16. I don't want to use this UIScreen.main.bounds.width. I want it to be dynamic.

Do you guys have any idea how to do this?

Thank you!

Code sample

By using static value it works

struct TestButton : View {
    var body: some View {
        Button(action: {

        }) {
            Text("Tap me")
        }
        .modifier(PrimaryButton())
    }
}

fileprivate struct PrimaryButton: ViewModifier {
    func body(content: Content) -> some View {
        content
            .frame(width: 300, height: 28)
            .padding()
            .background(Color.yellow)
            .foregroundColor(Color.white)
    }
}

By using dfd's comment, does not change anything.

struct TestButton : View {
    var body: some View {
        Button(action: {

        }) {
            Text("Tap me")
        }
        .modifier(PrimaryButton())
    }
}

fileprivate struct PrimaryButton: ViewModifier {
    func body(content: Content) -> some View {
        content
            .relativeWidth(1.0)
            .padding()
            .background(Color.yellow)
            .foregroundColor(Color.white)
    }
}

Upvotes: 1

Views: 2416

Answers (2)

ReflectCode
ReflectCode

Reputation: 1

Have you tried .frame(minWidth: 0, maxWidth: .infinity)

Setting the frame width to ".infinity" makes it use maximum width

SwiftUI Button fill parent

Here is the complete code -

import SwiftUI

struct ContentView: View {

    var body: some View {
        VStack(alignment: .leading, spacing: CGFloat(5)){

            Button(action: {}) {
                Text("Button fill width")
                    .font(.title)
                    .foregroundColor(.white)
            }.padding()
            .frame(minWidth: 0, maxWidth: .infinity)
            .background(Color.gray)

            HStack(alignment: .center, spacing: CGFloat(5)){

                Button(action: {}) {
                    Text("Btn-1")
                        .font(.title)
                        .foregroundColor(.white)
                }.padding()
                .background(Color.gray)

                Button(action: {}) {
                    Text("Button fill width")
                        .font(.title)
                        .foregroundColor(.white)
                }.padding()
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.gray)

            }

            HStack(alignment: .center, spacing: CGFloat(5)){

                Button(action: {}) {
                    Text("B-1")
                        .font(.title)
                        .foregroundColor(.white)
                }.padding()
                .background(Color.gray)

                Button(action: {}) {
                    Text("B fill")
                        .font(.title)
                        .foregroundColor(.white)
                }.padding()
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.gray)

                Button(action: {}) {
                    Text("B fill")
                        .font(.title)
                        .foregroundColor(.white)
                }.padding()
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.gray)
            }

            HStack(alignment: .center, spacing: CGFloat(5)){
                Spacer()
                Text("Hello World...")
                    .font(.title)
                    .fontWeight(.heavy)
                    .frame(minWidth: 0, maxWidth: .infinity)
                    .padding(5)
                }.background(Color.gray)

            HStack(alignment: .center, spacing: CGFloat(5)){
                Spacer()
                Text("Hello World...")
                    .font(.body)
                    .fontWeight(.heavy)
                    .multilineTextAlignment(.leading)
                    .lineLimit(10)
                    .padding(5)
                }.background(Color.gray)


            HStack(alignment: .center, spacing: CGFloat(5)){
            Button(action: {}) {
                Text("1").foregroundColor(.black).bold()
                }.padding()
            .frame(minWidth: 0, maxWidth: .infinity)
            .background( Image("RC_btn_default_normal")
            .renderingMode( Image.TemplateRenderingMode.original))

            Button(action: {}) {
                Text("2").foregroundColor(.black).bold()
            }.padding()
            .frame(minWidth: 0, maxWidth: .infinity)
            .background( Image("RC_btn_default_normal")
            .renderingMode( Image.TemplateRenderingMode.original))

            Button(action: {}) {
                Text("3").foregroundColor(.black).bold()
            }.padding()
            .frame(minWidth: 0, maxWidth: .infinity)
           .background(Image("RC_btn_default_normal")
            .renderingMode( Image.TemplateRenderingMode.original))

            Button(action: {}) {
                Text("4").foregroundColor(.black).bold()
            }.padding()
            .frame(minWidth: 0, maxWidth: .infinity)
            .background( Image("RC_btn_default_normal")
            .renderingMode( Image.TemplateRenderingMode.original))
            }
            Spacer()
        }.background(Color.green)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 0

Radagast
Radagast

Reputation: 318

GeometryReader may help you

for example:

SomeButton: View {
    var body: some View {
        GeometryReader { geometry in
            VStack() {
                Button(action:{}) {
                    Text("\(geometry.size.width)")
                }.padding()
                .frame(minWidth: geometry.frame(in: .global).size.width,
                        minHeight: geometry.frame(in: .global).size.height
                )
                .background(Color.red)
            }
        }
    }
}

Upvotes: 2

Related Questions