Tyler Pashigian
Tyler Pashigian

Reputation: 497

Get or Set leading spacing of Navigation Title on Large Title Mode - SwiftUI

I am trying to make sure the Navigation Title (in large title display mode) and various headings/elements below on the page of my iOS app line up, essentially having the same leading padding. The two approaches I can think of are:

  1. Get the value of the default leading padding on the Navigation Title and save that value to specify the leading padding of other elements on the page
  2. Override the default leading spacing between of the Navigation title.

Essentially get/set the spacing represented by the red line in the attached image below. I haven't been successful in either of the options. Does anyone know how to do this?

Upvotes: 21

Views: 2475

Answers (2)

Gavin Morrow
Gavin Morrow

Reputation: 769

I don't know how to get the actual value, but I believe that it's just the default padding amount.

import SwiftUI

struct ContentView: View {
    @State private var text: String = ""
    @State private var searchString = ""
    
    var body: some View {
        NavigationView {
            VStack(alignment: .leading) {
                Text("some text")
                List {
                    Text("Item 1")
                }
                .searchable(text: $searchString)
                Text("some more text")
            }
            .padding(.horizontal)
            .navigationTitle("Title")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 2

joshuakcockrell
joshuakcockrell

Reputation: 6153

Changing large NavigationTitle padding

This can be set across your entire app in the ApplicationDelegate

UINavigationBar.appearance().layoutMargins.left = 80

Full example code

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
    ) -> Bool {
        // Adjust left margin
        UINavigationBar.appearance().layoutMargins.left = 80
        
        return true
    }
}

@main
struct testApp: App {
    // Make sure you attach the AppDelegate
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Upvotes: 5

Related Questions