< How to hide and show the sidebar programmatically | How to customize the display mode of NavigationSplitView > |
Updated for Xcode 14.2
SwiftUI’s NavigationSplitView
uses a system-standard width for the view it presents, but with the navigationSplitViewColumnWidth()
modifier you can attempt to customize this.
Note: The system can choose to ignore the width you specify. At the time of writing this modifier is ignored on iPhone, and on iPad only works for values lower than the default size.
In its simplest form, sending a single value to navigationSplitViewColumnWidth()
causes it to use a fixed size, no smaller or larger:
NavigationSplitView {
Text("Sidebar")
.navigationSplitViewColumnWidth(100)
} content: {
Text("Content")
.navigationSplitViewColumnWidth(200)
} detail: {
Text("Detail")
}
Download this as an Xcode project
However, if you’re happy to allow flexibility – and if you’re platform supports it, which right now might just be macOS – you an provide minimum, ideal, and maximum sizes instead, like this:
NavigationSplitView {
Text("Sidebar")
.navigationSplitViewColumnWidth(min: 100, ideal: 200, max: 300)
} content: {
Text("Content")
.navigationSplitViewColumnWidth(min: 100, ideal: 200, max: 300)
} detail: {
Text("Detail")
}
Download this as an Xcode project
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Link copied to your pasteboard.