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

iPad端Document-based App显示两个返回箭头的问题求助

多平台文档App在iPad上的导航箭头问题

问题场景与现象

  • 基于Xcode 14.0.1的多平台文档App模板开发,自定义包文件格式,目标适配macOS与iPad
  • macOS端运行正常,iPad端出现两个问题:
    • 打开App直接唤起文件选择器
    • 打开/新建文件后,界面出现两个返回箭头:左侧较大的箭头无响应,右侧箭头可重新唤起文件选择器
  • 备注:示例App无自定义导航逻辑,排除自定义代码导致该问题的可能

疑问

  1. 左侧无响应的大箭头是什么?
  2. 如何移除这个箭头?
  3. 该问题是否属于SwiftUI框架问题,需要向Apple反馈?

完整代码

import SwiftUI

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

import UniformTypeIdentifiers

extension UTType {
    static var appfroschFile: UTType {
        UTType(importedAs: "ch.appfros.so-DocumentAppWithNavigationShowsMultipleChevrons")
    }
}

struct so_DocumentAppWithNavigationShowsMultipleChevronsDocument: FileDocument {
    var document: Document

    init(document: Document = Document(text: "Hello, world!")) {
        self.document = document
    }

    static var readableContentTypes: [UTType] { [.appfroschFile] }

    init(configuration: ReadConfiguration) throws {
        guard let fileWrappers = configuration.file.fileWrappers
        else {
            throw CocoaError(.fileReadCorruptFile)
        }
        guard let documentFileWrapper = fileWrappers["document"],
              let data = documentFileWrapper.regularFileContents,
              let string = String(data: data, encoding: .utf8)
        else {
            throw CocoaError(.fileReadCorruptFile)
        }
        document = try JSONDecoder().decode(Document.self, from: data)
    }
    
    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
        let data = try JSONEncoder().encode(document)
        let documentFileWrapper = FileWrapper(regularFileWithContents: data)
        
        let mainFileWrapper = FileWrapper(directoryWithFileWrappers: [
            "document": documentFileWrapper
        ])
        return mainFileWrapper
    }
}

struct Document: Codable {
    var text: String
}

struct ContentView: View {
    @Binding var document: so_DocumentAppWithNavigationShowsMultipleChevronsDocument

    var body: some View {
        TextEditor(text: $document.document.text)
    }
}

解答

1. 左侧箭头的本质

左侧无响应的大箭头是iPad端DocumentGroup组件默认添加的导航栏返回按钮。由于DocumentGroup在iPad的UI层级中已经嵌套了一层导航栈,而你的ContentView未明确配置导航栏显示逻辑,导致系统自动生成的返回按钮和右侧系统自带的文档管理按钮(唤起文件选择器)重复出现。

2. 移除方法

直接在ContentView中隐藏默认返回按钮即可,修改代码如下:

struct ContentView: View {
    @Binding var document: so_DocumentAppWithNavigationShowsMultipleChevronsDocument

    var body: some View {
        TextEditor(text: $document.document.text)
            .navigationBarBackButtonHidden(true) // 隐藏默认返回按钮
    }
}

如果不需要整个导航栏,也可以直接隐藏:

.navigationBarHidden(true)

3. 是否属于框架问题?

当前问题属于UI配置层级的问题,通过上述代码调整即可解决,并非SwiftUI框架bug,无需向Apple反馈。如果调整后仍出现异常,再考虑通过Feedback Assistant提交反馈。

另外,iPad端打开App直接唤起文件选择器是文档类App的默认系统行为,符合Apple的文档App交互规范,若需修改可尝试在DocumentGroup中配置相关参数,但需注意遵循平台规范。

内容的提问来源于stack exchange,提问作者appfrosch

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 12:35:27