GitXplorerGitXplorer
a

NavigationTitle

public
5 stars
2 forks
0 issues

Commits

List of commits on branch main.
Unverified
2e72ce14d9bc9f01e9585ee382191e7d1a0f7a0c

indicate deprecated

aamonshiz committed 4 years ago
Verified
f35c6701171d1ceb466b4137716d1a35b91b37cf

Update README.md

aamonshiz committed 4 years ago
Unverified
e06820d43d2a4892d8d8de4b4b89caebe1c72da5

Add promo video

aamonshiz committed 4 years ago
Unverified
404b236b2f8e3f321daba5f6b3e3fb228e922bd9

Complete rename to NavigationTitle

aamonshiz committed 4 years ago
Unverified
23cae37c9a6b0cb1df38da949854408d6ac168fa

Rename to NavigationTitle

aamonshiz committed 4 years ago
Unverified
fff1902c9ac8f8efc5ea3bcbdd66800b9d8330b6

Update README

aamonshiz committed 4 years ago

README

The README file for this repository.

DEPRECATED

Xcode 12 beta 5 introduced functionality that makes this no longer necessary. Please do not use. :)

NavigationTitle

Package that lets a navigationTitle and a ToolbarItemPlacement.principal work together

Make your dreams come true!

PhoneRecentsTab

Installation

Another Swift package

Add to your Package.swift file

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

Add to an app

  • File -> Swift Packages -> Add Package Dependency ...
  • https://github.com/amonshiz/NavigationTitle.git
  • Track the main branch (I make no promises on keeping tags up to date)

Usage

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)
  }
}

Notes

  • It is best practice to place the .navigationTitle(_:within:) as close to the top level of each view as possible. Ideally it is applied to the view that is a direct child of NavigationView.
  • It is best practice to use separate View types as the destination for a NavigationLink as opposed to defining the destination inline. This ensures that the order of evaluating the View.body property matches the order that they will be displayed.
  • You can mix and match the system .navigationTitle() with this, it has been tested but there may be edges cases. If so please submit an Issue!

Problem

In SwiftUI there is the convenient toolbar and toolbarItem APIs that allow the developer to add content to toolbars and navigation bars easily and to define the semantics of where that content should appear within the bar. Additionally, SwiftUI makes it extremely easy to change the navigation title display mode with navigationBarTitleDisplayMode. However, there is at least one conflict between the display mode and toolbar item API: with a large or automatic display mode any content put in the principal placement will not be displayed at all.

The use case is something like the "Recents" tab in the Phone.app UI. Phone.app UI for Recents tab

Solution

Use UIViewConrollerRepresentable to hook directly into the underlying UIKit objects and insert the desired text into the view controller and its attendant navigationItem. In this case the package is replacing the UINavigationControllerDelegate instance in the "root" navigation controller with an internal implementation that sets the title on any pushed view controller that doesn't already have a title and it's navigationItem. This internal delegate also maintains a weak reference to any existing delegate and will forward all messages along to it as necessary.

Issues

  • Does not work with AppKit or watchOS yet
  • There is a bug in SwiftUI that causes an [Assert] to fire after pushing two views onto a navigation stack (feedback documented here) so just know that isn't caused by this!