在SwiftUI的大标题模式下获取或设置导航标题的前导空格

17
我正在努力确保我的iOS应用程序的页面上导航标题(在大标题显示模式下)和下面的各种标题/元素排列整齐,基本上具有相同的前导填充。我能想到的两种方法是:
  1. 获取导航标题默认前导填充的值,并保存该值以指定页面上其他元素的前导填充
  2. 覆盖导航标题之间的默认前导间距。
基本上是获取/设置附加图像中红线所表示的间距。我在这两个选项中都没有成功。有人知道如何做到这一点吗?

2
我不知道为什么有人踩了你的问题 - 我不知道答案,但对我来说这个问题很好;) 我也对答案感兴趣;) - Chris
2个回答

2
我不知道如何获得实际值,但我相信这只是默认填充量。
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()
    }
}

1

更改大型导航标题填充

您可以在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()
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接