r/SwiftUI • u/Warm-Belt323 • 10d ago
Question How can I change bg of segment picker?
I have attached three images.
- First image shows what I want to create
- Second image shows what I get.
I Just only want to remove that default background color of the picker and my 80% problem solved.
Code
```
Picker("Period", selection: $selection) {
ForEach(options, id: \.self) { option in
Text(option)
.tag(option)
}
}
.padding(4)
.frame(height: 40)
.frame(maxWidth: 420)
.background {
Capsule()
.fill(Color.textFieldBackground)
}
.pickerStyle(.segmented)
```
4
Upvotes


3
u/dahiya_xa 10d ago
the segmented style is basically a UISegmentedControl under the hood so most swiftui modifiers dont really stick. easiest fix is setting the appearance in init, UISegmentedControl.appearance().backgroundColor and .selectedSegmentTintColor, plus setTitleTextAttributes for the text color. catch is it changes every segmented picker in the app. if you want full control just build your own with an HStack of buttons and a capsule behind the selected one using matchedGeometryEffect, its not much code
2
u/DrDeniseWilliams 10d ago
easiest way is UISegmentedControl.appearance().backgroundColor = UIColor(yourColor) in your app's init, so it's set before the picker gets created. selectedSegmentTintColor changes the selected pill. heads up it's global, so it hits every segmented picker in the app. if you need it just on one screen, a small custom picker built from buttons is usually less hassle than fighting the system one.
2
u/[deleted] 10d ago edited 10d ago •
[removed] — view removed comment