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.
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!
DEPRECATED
Xcode 12 beta 5 introduced functionality that makes this no longer necessary. Please do not use. :)
NavigationTitle
Package that lets a
navigationTitle
and aToolbarItemPlacement.principal
work togetherMake your dreams come true!
Installation
Another Swift package
Add to your
Package.swift
fileAdd to an app
https://github.com/amonshiz/NavigationTitle.git
main
branch (I make no promises on keeping tags up to date)Usage
Notes
.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 ofNavigationView
.View
types as thedestination
for aNavigationLink
as opposed to defining the destination inline. This ensures that the order of evaluating theView.body
property matches the order that they will be displayed..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
andtoolbarItem
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 withnavigationBarTitleDisplayMode
. However, there is at least one conflict between the display mode and toolbar item API: with alarge
orautomatic
display mode any content put in theprincipal
placement will not be displayed at all.The use case is something like the “Recents” tab in the Phone.app UI.
Solution
Use
UIViewConrollerRepresentable
to hook directly into the underlying UIKit objects and insert the desired text into the view controller and its attendantnavigationItem
. In this case the package is replacing theUINavigationControllerDelegate
instance in the “root” navigation controller with an internal implementation that sets thetitle
on any pushed view controller that doesn’t already have a title and it’snavigationItem
. This internal delegate also maintains aweak
reference to any existing delegate and will forward all messages along to it as necessary.Issues
[Assert]
to fire after pushing two views onto a navigation stack (feedback documented here) so just know that isn’t caused by this!