Reputation: 497
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:
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
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
Reputation: 6153
This can be set across your entire app in the ApplicationDelegate
UINavigationBar.appearance().layoutMargins.left = 80
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