4

I'm trying to change the background of a segmented control to clear but it isn't working. If I change it to a bright color like orange or blue, the color changes, but clear still leaves me with a shade of gray as a background color?

struct HomeSwipeView: View {
    
    init() {
        
        UISegmentedControl.appearance().backgroundColor = .clear
        UISegmentedControl.appearance().setTitleTextAttributes([
            .font : UIFont.preferredFont(forTextStyle: .headline)
        ], for: .normal)
        
    }
    
    var body: some View {
        Picker("", selection: $selectedTabIndex) {
            Text("One").tag(0)
            Text("Two").tag(1)
            
        }.pickerStyle(SegmentedPickerStyle())
    }
}
aheze
  • 24,434
  • 8
  • 68
  • 125
GarySabo
  • 5,806
  • 5
  • 49
  • 124
  • Try not setting the background at all, what color does it become in that case? Also, try setting it to alpha zero from a `UIcolor` instead of `.clear`. Also, try to explain what you have done to look for an answer online. Mabe that you googled or something rather than nothing. – a1cd May 03 '21 at 23:33
  • @Evergreen yes I searched SO extensively before I posted, every answer says to set it the way I am in `init` which is why I am perplexed. Using `UIColor.clear.withAlphaComponent(0.0)` gives the same result, the default background without setting it at all. – GarySabo May 04 '21 at 00:28
  • 4
    You can't change this via appearance or color properties, because it is drawn by layers of UIImageViews which compose segmented control. It is how UISegmentedControl constructed, so to remove this you need to replace layer of each segment in instance of UISegmentedControl. It is reverse-engineering and even if you do that it might change in next iOS update. I think it is simpler and safer to create something custom or use standard as-is. – Asperi May 04 '21 at 05:07
  • Just roll your own like this: https://stackoverflow.com/questions/66199531/change-segmented-controller-properties-in-swiftui/66203525#66203525. – Helperbug May 09 '21 at 06:48

0 Answers0