iTranslated by AI
Implementing Xcode-style Segmented Controls in SwiftUI Inspectors
The UI I want to achieve
In this article, I will implement a UI where a segmented control is placed on an inspector, similar to Xcode, using SwiftUI.

Using Picker
A naive implementation using Picker looks like this:
Picker("Inspector View Mode", selection: $suit) {
ForEach(Suit.allCases) { suit in
Image(systemName: suit.systemImageName)
}
}
.pickerStyle(.segmented)
.labelsHidden()
Full Implementation
import SwiftUI
struct ContentView: View {
@State var isPresented = true
var body: some View {
NavigationSplitView {
Text("Sidebar")
} detail: {
Text("Canvas")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button("Inspector", systemImage: "sidebar.trailing") {
isPresented.toggle()
}
}
}
.inspector(isPresented: $isPresented) {
InspectorView()
}
}
}
}
struct InspectorView: View {
@State private var suit: Suit = .heart
var body: some View {
ScrollView {
Text("Inspector")
.frame(maxWidth: .infinity, alignment: .leading)
.padding()
}
.safeAreaBar(edge: .top) {
Picker("Inspector View Mode", selection: $suit) {
ForEach(Suit.allCases) { suit in
Image(systemName: suit.systemImageName)
}
}
.pickerStyle(.segmented)
.labelsHidden()
}
.tint(.green)
}
enum Suit: String, CaseIterable, Identifiable {
var id: Self { self }
case heart, club, diamond, spade
var systemImageName: String {
switch self {
case .heart: "suit.heart"
case .club: "suit.club"
case .diamond: "suit.diamond"
case .spade: "suit.spade"
}
}
}
}
The result of this implementation looks like this:
Although it has slightly rounded corners, it is not capsule-shaped, which is not what I was aiming for.

Adding controlSize
Revisiting the WWDC session video Build a SwiftUI app with the new design, it was explained that I should use controlSize. Indeed, WWDC session videos are the best place to look when you get stuck.

Quoted from Build a SwiftUI app with the new design
Based on that, I added controlSize:
Picker("Inspector View Mode", selection: $suit) {
ForEach(Suit.allCases) { suit in
Image(systemName: suit.systemImageName)
}
}
.pickerStyle(.segmented)
.labelsHidden()
.controlSize(.large) // Added
This result looks as follows. It has rounded edges, but it looks compact. I want it to expand to fill the width.

I got a bit stuck here.
Adding buttonSizing
While wondering what to do, I suddenly thought, "This segmented control is also a kind of button," and "If it's an iOS button, I can change the width with buttonSizing." So, on a whim, I tried using buttonSizing.
Picker("Inspector View Mode", selection: $suit) {
ForEach(Suit.allCases) { suit in
Image(systemName: suit.systemImageName)
}
}
.pickerStyle(.segmented)
.labelsHidden()
.controlSize(.large)
.buttonSizing(.flexible) // Added
Here is the result:

It looks very close!
Final UI
Finally, I added horizontal padding to get the following. Doesn't it look good?
Picker("Inspector View Mode", selection: $suit) {
ForEach(Suit.allCases) { suit in
Image(systemName: suit.systemImageName)
}
}
.pickerStyle(.segmented)
.labelsHidden()
.controlSize(.large)
.buttonSizing(.flexible)
.padding(.horizontal, 8)

Conclusion
In SwiftUI, it is often ambiguous which ViewModifier is valid for which View, leading to confusion about "how to achieve that UI." So, I decided to document it in this article.
I hope this article helps someone.
Discussion