底部Sheet内NavigationStack标题设.large仍显示为inline的问题求助
方案1:调整修饰符应用位置
把navigationBarTitleDisplayMode(.large)直接应用到NavigationStack上,而非内部的Form,让导航栈初始就识别大标题模式:
NavigationStack { Form { // 你的表单内容 } .navigationTitle("Title") } .navigationBarTitleDisplayMode(.large)
方案2:手动触发滚动(兼容更多场景)
如果方案1无效,可在Form的onAppear回调里,让滚动视图轻微偏移,触发大标题展开逻辑:
NavigationStack { Form { // 你的表单内容 } .navigationTitle("Title") .navigationBarTitleDisplayMode(.large) .onAppear { DispatchQueue.main.async { // 获取当前表单对应的滚动视图 guard let scrollView = UIApplication.shared.windows.first?.rootViewController?.presentedViewController?.view.subviews.compactMap({ $0 as? UIScrollView }).first else { return } // 轻微偏移触发大标题展开 scrollView.setContentOffset(CGPoint(x: 0, y: -1), animated: false) } } }
方案3:全局设置(不推荐,除非全App需要大标题)
如果你的应用所有页面都默认需要大标题,可在App初始化时设置全局导航栏外观:
@main struct YourApp: App { init() { UINavigationBar.appearance().prefersLargeTitles = true } var body: some Scene { WindowGroup { ContentView() } } }
之后只需在页面设置navigationTitle,无需额外指定显示模式。
内容的提问来源于stack exchange,提问作者helloimbrando
相关产品推荐
相关产品推荐

