2

Working in swiftui. I have a toggle button and a text with it

struct ContentView: View {
    
    @State private var showToggle = true
    
    var body: some View {
        Toggle("Toggle", isOn: $showMyIssues)
    }
}

It seems the default is for the text to be on the leading edge of its width and the button to be at the trailing edge of the width *See photo

enter image description here

I have tried setting the alignment to both leading and trailing, but neither the button nor the text seem to adjust..

I need the text to be right next to the toggle button and to be able to set the alignment for the toggle and the text together while the text remains next to the toggle button.

*UPDATE I tried following the answer in this SwiftUI - how to change text alignment of label in Toggle? question. This brings the text to the the trailing edge near the toggle button, which fixes half my problem. But now there is not an easy way to move both the text and the toggle button to the leading (left) edge of the screen. They are stuck to the right

    var body: some View {
        Toggle(isOn: $showMyIssues) {
            Text("My Issues")
                .frame(maxWidth: .infinity, alignment: .trailing)
        }

enter image description here

texasRanger009
  • 139
  • 1
  • 9

1 Answers1

0

use something like this:

 Toggle("Toggle", isOn: $showToggle).frame(width: 130)

you can then use a HStack to further adjust the position.

  • 2
    I'm gonna say that this is technically correct, but in practice, the worst possible solution you can use for this lol. Hard coding a width will just lead to layout issues later on. – xTwisteDx Aug 12 '22 at 00:20