Search code examples
iosswiftui

SwiftUI iOS - change toggle size


    Toggle(isOn: self.$autoplay) {
    
        Button(action: {
    
            autoplay.toggle()
        }) {
            HStack(spacing: 26) {
    
                Image(systemName: "play.circle")
                    .resizable()
                    .scaledToFit()
                    .frame(width: iconSize, height: iconSize)
    
                Text("video_autoplay")
                    .padding(.trailing, 26)
    
            }
            .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
        }
    }
    .toggleStyle(.switch)
    .controlSize(.mini)

This is what I'm using based on the answer here:

Styling a small toggle switch in SwiftUI like the ones in System Settings on macOS

why isn't it working?


Solution

  • A workaround for changing the size of Toggle is to use scaleEffect. The drawback is that you must also set frame and offSet to make the Toggle fit with the design:

    Toggle(isOn: $autoplay) {
    }
    .background {
        Color.yellow.opacity(0.3)
    }
    .fixedSize()
    .scaleEffect(0.8)
    .offset(x: 5)
    

    enter image description here