Skip to content

Latest commit

 

History

History
80 lines (54 loc) · 2.56 KB

File metadata and controls

80 lines (54 loc) · 2.56 KB

Navigation Patterns 🕹

Modal

TableView

Modal ViewControllers are presented modally to uses on the screen.

@objc func presentPressed() {
    let viewController = ModalViewController2()
    present(viewController, animated: true, completion: nil)
}

@objc func dismissPressed() {
    dismiss(animated: true, completion: nil)
}

Container

TableView

Containers (UINavigationController, UITabBarController, UIPageViewController) are ViewControllers Apple has made to make our lives easier for doing default navigation in UIKit.

UINavigationController and UITabBarController are so core to UIViewController that they are actual properties you can access. You pusn and pop off a UINavigationController like this.

@objc func pushPressed() {
    if let navigationController = navigationController {
        let viewController = NavigationViewController2()
        navigationController.pushViewController(viewController, animated: true)
    }
}

@objc func popPressed() {
    if let navigationController = navigationController {
        navigationController.popViewController(animated: true)
    }
}

Custom

TableView

For those cases where Apple's container viewControllers don't cut it, we can create our own. Here we need make sure we follow these three steps whenever we present or dismiss a new viewController embedded in another.

func presentNextState(viewController: UIViewController) {
    // The view controller we want to present embedded in our navigationViewController
    navigationViewController.setViewControllers([nextViewController], animated: true)

    //
    // x3 things we need to do when adding child view Controller
    //

    // 1. Move the child view controller's view to the parent's view.
    view.addSubview(navigationViewController.view)

    // 2. Add the view controller as a child.
    addChild(navigationViewController)

    // 3. Notify the child that it was moved to a parent.
    navigationViewController.didMove(toParent: self)
}

Links that help

Video