已弃用

Xcode 12 beta 5 引入了新的功能,使得本软件包不再必要。请不要使用。:)

NavigationTitle

此软件包让 navigationTitleToolbarItemPlacement.principal 能够协同工作

让你的梦想成真!

PhoneRecentsTab

安装

另一个 Swift 软件包

添加到你的 Package.swift 文件中

let package = Package(
  ...
  dependencies = [
    .package(url: "https://github.com/amonshiz/NavigationTitle.git", Package.Dependency.Requirement.branch("main")),
    ...
  ],
  targets = [
    .target(
      name: "YourTarget",
      dependencies: ["NavigationTitle", …],
      ...),
    ...
  ]
)

添加到应用

用法

import SwiftUI
import NavigationTitle

struct ContentView: View {
  @Namespace var aNamespace // required

  var body: some View {
    NavigationView {
      // Does not need to be a list, but this is does demonstrate the `.large`
      // style title behaviors best
      List {
        Text("Hello, world").padding()
      }
      // Used in the same place the usual `.navigationTitle(_)` or
      // `.navigationBarTitle(_)` would be used
      .navigationTitle("A Great Title", within: aNamespace)
    }
    // Should be used *on* the navigation stack to track
    .rootNavigationBarIdentified(within: aNamespace)
  }
}

注意

问题

在 SwiftUI 中,有方便的 toolbartoolbarItem API,允许开发者轻松地将内容添加到工具栏和导航栏,并定义该内容应出现在栏中的语义位置。此外,SwiftUI 使得使用 navigationBarTitleDisplayMode 更改导航标题显示模式非常容易。然而,显示模式和工具栏项目 API 之间至少存在一个冲突:对于 largeautomatic 显示模式,放置在 principal 位置的任何内容都不会显示。

用例类似于 Phone.app UI 中的 "Recents" 选项卡。 Phone.app UI for Recents tab

解决方案

使用 UIViewConrollerRepresentable 直接连接到下面的 UIKit 对象,并将所需的文本插入到视图控制器及其相关的 navigationItem 中。在这种情况下,该软件包正在用一个内部实现替换“根”导航控制器中的 UINavigationControllerDelegate 实例,该内部实现设置任何被推送的且尚未拥有标题的视图控制器的 title 和它的 navigationItem。这个内部委托还维护了对任何现有委托的 weak 引用,并将根据需要将所有消息转发给它。

问题