3

Is there any way to change the height of segmented controller in SwiftUI or it can be achieved with only creating custom segmented controller? I tried .frame(height) but nothing has changed. I need to increase the height of segmented controller.

@State private var userType = 0

init() {
    UISegmentedControl.appearance().selectedSegmentTintColor = .white
    UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.blue], for: .selected)
    UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.lightGray], for: .normal)
}

var body: some View {
    VStack {

    HStack {
        Picker("Choose user type", selection: $userType) {
            Text("User")
                .tag(0)
            Text("Administrator")
                .tag(1)
        }
        .pickerStyle(.segmented)
        .frame(height: 40)
        .padding(.horizontal, 16)
        }
}}

enter image description here

Abrcd18
  • 155
  • 1
  • 13
  • 1
    This is standard control - not customisable (at least for now). If you don't like standard - create custom. – Asperi Jan 13 '22 at 08:13

1 Answers1

2

You can override function didMoveToSuperview:

extension UISegmentedControl {
  override open func didMoveToSuperview() {
     super.didMoveToSuperview()
     self.setContentHuggingPriority(.defaultLow, for: .vertical)  
   }
}

After set height for Picker

 Picker("", selection: $tab) {
   Text("Tab 1")
       .font(getFont(15))
       .tag(.tab1)
                      
   Text("Tab 2")
       .font(getFont(15))
       .tag(.tab2)
               
 }.frame(height: 30)
  • I want to make it height lower than 30px, like 15px for example. It works perfectly for 30, but when I set 15 or less it just remains the same.. iOS 16. – KAMIKAZE May 11 '23 at 19:04