NavigationStackModel

public class NavigationStackModel<IdentifierType> : ObservableObject where IdentifierType : Equatable
extension NavigationStackModel: CustomDebugStringConvertible

The underlying NavigationStackView‘s model which can be manipulated to apply the navigation transitions.

This class is generic to provide the possibility to use a different type as identifier. However, normally you want to bound the identifier’s type simply to a string. For this you can also just use NavigationModel.

An instance of this model has to be injected into the view hierarchy as an environment object: MyRootView().environmentObject(NavigationModel()) Even when using multiple navigation stack views in a view hierarchy there has to be always only one instance of NavigationModel. The model can be used with identifiers to target specific navigation stack views.

  • Initializes the model.

    Declaration

    Swift

    public init(silenceErrors: Bool = false)

    Parameters

    silenceErrors

    When set to true each error will be silently ignored, when false each error will result in an exception thrown. Defaults to false.

  • Flag used to determine if errors are thrown or silently ignored.

    Declaration

    Swift

    public let silenceErrors: Bool
  • Performs the navigation by showing a new view.

    This is typically used to navigate to a new view.

    Warning

    It’s not possible to navigate to a new view while a navigation for the same stack view is already active, i.e. tying to push View3 on View1 when there is already View2 pushed on View1 will result in an error.

    Declaration

    Swift

    public func showView<Content: View>(
    	_ identifier: IdentifierType,
    	animation: NavigationAnimation? = nil,
    	@ViewBuilder alternativeView: @escaping () -> Content
    )

    Parameters

    identifier

    The navigation stack view’s identifier targeting by this navigation. The provided ID will be used to determine which navigation stack view should replace its view with the provided one.

    animation

    The transition animation to apply.

    alternativeView

    The new view which should replace the navigation stack view’s default view.

  • Navigates back to the previews view by using its reverse animation. This is typically used to execute a back navigation.

    Declaration

    Swift

    public func hideTopViewWithReverseAnimation()
  • Navigates back to the previous view by using a provided a specific transition animation. This is typically used to navigate back, but with a specific animation which might not be known in advance.

    Declaration

    Swift

    public func hideTopView(animation: NavigationAnimation? = nil)

    Parameters

    animation

    The transition animation to use for this navigation. When nil is passed then no animation will be used.

  • Navigates back to a specific navigation stack view somewhere in the stack. This is typically used to navigate back multiple views.

    Declaration

    Swift

    public func hideView(_ identifier: IdentifierType, animation: NavigationAnimation? = nil)

    Parameters

    identifier

    The navigation stack view’s identifier targeting by this navigation. The provided ID will be used to determine which navigation stack view should switch back to its default view.

    animation

    The transition animation to use during this transition. When nil is passed then no animation will be used.

  • Navigates back to a specific navigation stack view somewhere in the stack by using its reverse animation. This is typically used to execute a back navigation to a view farther down the stack, e.g. back to the root.

    Declaration

    Swift

    public func hideViewWithReverseAnimation(_ identifier: IdentifierType)

    Parameters

    identifier

    The navigation stack view’s identifier targeting by this navigation. The provided ID will be used to determine which navigation stack view should switch back to its default view.

  • Returns whether there is a navigation view on the stack or not, meaning is it possible to navigate back or not.

    True when it’s safe to navigate back, otherwise false.

    Warning

    Using this method to show different views or sub-views without freezing the result in a let variable will result in animation glitches!

    Declaration

    Swift

    public var hasAlternativeViewShowing: Bool { get }
  • Returns whether there is a navigation view with a specific ID on the stack or not, meaning is it possible to navigate to the navigation stack view or not.

    Warning

    Using this method to show different views or sub-views without freezing the result in a let variable will result in animation glitches!

    Declaration

    Swift

    public func isAlternativeViewShowing(_ identifier: IdentifierType) -> Bool

    Parameters

    identifier

    The navigation stack view’s identifier to query for its existence in the stack.

    Return Value

    True when it’s safe to navigate to the ID, otherwise false.

  • Creates and returns a binding for the top navigation stack view’s showing flag.

    This binding can be used to pass it to the new view shown by the navigation so the new view can dismiss itself by toggling the binding’s value.

    Declaration

    Swift

    public func topViewShowingBinding() -> Binding<Bool>

    Return Value

    The binding bound to the top view on the navigation stack.

  • Creates and returns a binding for a navigation stack view’s showing flag.

    • identifier: The navigation stack view’s identifier for which to create a binding.

    Declaration

    Swift

    public func viewShowingBinding(_ identifier: IdentifierType) -> Binding<Bool>

    Return Value

    The binding bound to the view on the navigation stack.

Debugging

  • Declaration

    Swift

    public var debugDescription: String { get }
  • A convenience method to navigate to a new view with a push transition animation.

    Declaration

    Swift

    func pushContent<Content>(_ identifier: IdentifierType, @ViewBuilder alternativeView: @escaping () -> Content) where Content : View

    Parameters

    identifier

    The navigation stack view’s ID on which to push.

    alternativeView

    The content view to push.

  • A convenience method to navigate back to a previous view with a pop transition animation.

    Declaration

    Swift

    func popContent(_ identifier: IdentifierType)

    Parameters

    identifier

    The navigation stack view’s ID on which to pop back.

  • A convenience method to navigate to a new view with a present transition animation.

    Declaration

    Swift

    func presentContent<Content>(_ identifier: IdentifierType, @ViewBuilder alternativeView: @escaping () -> Content) where Content : View

    Parameters

    identifier

    The navigation stack view’s ID on which to present.

    alternativeView

    The content view to present.

  • A convenience method to navigate back to a previous view with a dismiss transition animation.

    Declaration

    Swift

    func dismissContent(_ identifier: IdentifierType)

    Parameters

    identifier

    The navigation stack view’s ID on which to dismiss.

  • A convenience method to navigate to a new view with a fade-in transition animation.

    Declaration

    Swift

    func fadeInContent<Content>(_ identifier: IdentifierType, @ViewBuilder alternativeView: @escaping () -> Content) where Content : View

    Parameters

    identifier

    The navigation stack view’s ID on which to fade-in.

    alternativeView

    The content view to fade-in.

  • A convenience method to navigate back to a previous view with a fade-out transition animation.

    Declaration

    Swift

    func fadeOutContent(_ identifier: IdentifierType)

    Parameters

    identifier

    The navigation stack view’s ID on which to fade-out.