You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FileDocument应用中.bottomBar布局的ToolbarItemGroup无法正常显示的问题求助

FileDocument应用中.bottomBar布局的ToolbarItemGroup无法正常显示的问题求助

各位大佬好,我最近在开发一个基于SwiftUI的FileDocument应用,碰到了个棘手的问题:我想把ToolbarItemGroup通过.bottomBar布局放在屏幕底部,但不管是iOS 18.6.1真机还是iOS 18.5模拟器,这个底部工具栏都死活不显示。

奇怪的是,如果换成.automatic这类其他布局值,工具栏就能正常出现;而且在ContentView的预览界面里,用.bottomBar布局也能正确显示出底部工具栏。

更头疼的是,我实际的大型项目里还出现了.bottomBar工具栏运行几分钟后随机消失的情况,但现在连这个简化后的示例问题都没搞明白,根本没法推进后续调试。有没有大佬能给点建议或者排查思路呀?

以下是我用来复现问题的示例代码:

import SwiftUI
import UniformTypeIdentifiers

@main
struct toolbar_debugApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: toolbar_debugDocument()) { file in
            ContentView(document: file.$document)
        }
    }
}

struct ContentView: View {
    @Binding var document: toolbar_debugDocument
    
    var body: some View {
        Text(document.text)
            .toolbar() {
                // 这个布局能正常显示
                ToolbarItemGroup(placement: .automatic) {
                    HStack{
                        Button("Open") { print("Open!")}
                        Spacer()
                        Button("Save") { print("Save!")}
                    }
                }
                // 换成.bottomBar就无法显示
                // ToolbarItemGroup(placement: .bottomBar) {
                //     HStack{
                //         Button("Open") { print("Open!")}
                //         Spacer()
                //         Button("Save") { print("Save!")}
                //     }
                // }
            }
    }
}

extension UTType {
    static var exampleText: UTType {
        UTType(importedAs: "com.example.plain-text")
    }
}

struct toolbar_debugDocument: FileDocument {
    var text: String
    
    init(text: String = "Hello, world!") {
        self.text = text
    }
    
    static var readableContentTypes: [UTType] {
        [.exampleText]
    }
    
    init(configuration: ReadConfiguration) throws {
        guard let data = configuration.file.regularFileContents,
              let string = String(data: data, encoding: .utf8) else {
            throw CocoaError(.fileReadCorruptFile)
        }
        text = string
    }
    
    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
        let data = text.data(using: .utf8)!
        return .init(regularFileWithContents: data)
    }
}

我自己也试了一些方向但没解决:

  • 确认.bottomBar的用法没写错,预览里是正常的
  • 检查了项目Info.plist的配置,没发现和工具栏相关的冲突项

有没有大佬遇到过类似问题?或者能指点下接下来该从哪些方向排查?


内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 07:34:51