NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to customize a view’s width in NavigationSplitView

Paul Hudson    @twostraws   

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

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.