我正在努力确保我的iOS应用程序的页面上导航标题(在大标题显示模式下)和下面的各种标题/元素排列整齐,基本上具有相同的前导填充。我能想到的两种方法是:
- 获取导航标题默认前导填充的值,并保存该值以指定页面上其他元素的前导填充
- 覆盖导航标题之间的默认前导间距。
![](https://istack.dev59.com/6G3BB.webp)
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()
}
}
您可以在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()
}
}
}