[Beginner] Implement NavigationBar in code

Introduction

I was researching various things to make an app without using Storyboard, but the navigation bar did not work even if I copied the code on the net.

I hope it helps people who are in the same situation as me, and I will put a solution.

environment

Implement a navigation bar without using StoryBoard

First, add the code to SceneDelegate.swift.

SceneDelegate.swift


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//By default_So rewrite it as a scene
        guard let scene = (scene as? UIWindowScene) else { return }
//firstViewController is the first screen displayed(rootViewController)
        let firstViewController = ViewController()
        let navigationController = UINavigationController(rootViewController: firstViewController)
      
        window = UIWindow(windowScene: scene)
        window?.rootViewController = navigationController
        window?.makeKeyAndVisible()
    }

window has moved from AppDelegate toSceneDelegate in the iOS 13 update, and even if you search, most of the information is adding code to AppDelegate. I think you can think of it as "I used to implement it with AppDelegate, but now I need to describe it in SceneDelegate".

Next, we'll write the code that implements the navigation bar.

ViewController.swift


class ViewController: UIViewController {
    
    var button: UIBarButtonItem!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        title = "First"
        button = UIBarButtonItem(barButtonSystemItem: .fastForward, target: self, action: #selector(click))
        navigationItem.rightBarButtonItem = button
    }
    
    @objc func click() {
        let second = SecondViewController()
        navigationController?.pushViewController(second, animated: true)
    }
    
}

SecondViewController.swift


class SecondViewController: UIViewController {

    var button: UIBarButtonItem!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Second"
        view.backgroundColor = .systemTeal
        
        button = UIBarButtonItem(barButtonSystemItem: .fastForward, target: self, action: #selector(click))
        navigationItem.rightBarButtonItem = button
    }
    
    @objc func click() {
        let third = ThirdViewController()
        navigationController?.pushViewController(third, animated: true)
    }

}

ThirdViewController.swift


class ThirdViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Third"
        view.backgroundColor = .purple
    }
}

I tried to put the transition movement twice to give a feeling of movement. By the way, this article is intended to implement a navigation bar, so I won't cover UIWindow or @ objc.

** It looks like this when you actually move it **

Recommended Posts

[Beginner] Implement NavigationBar in code
Implement CustomView in code
Implement Swift UITextField in code
Implement markdown in Rails
How to implement UICollectionView in Swift with code only
Implement application function in Rails
Implement follow function in Rails
Code entry method in Qiita
Implement two-step verification in Java
Implement LTI authentication in Rails
Implement Basic authentication in Java
Implement math combinations in Java
2 Implement simple parsing in Java
Java in Visual Studio Code
[Android] Implement Adblock in WebView
Implement Email Sending in Java
Write Java8-like code in Java8
Implement import process in Rails
Implement functional quicksort in Java
Implement rm -rf in Java.
Implement XML signature in Java
[Swift] Create UIButton class in code
Solve AtCoder Beginner Contest 151 in java
Implement Table Driven Test in Java 14
Implement textField focus-out event in JavaFX
Guess the character code in Java
Solve AtCoder Beginner Contest 150 in java
3 Implement a simple interpreter in Java
Try to implement Yubaba in Ruby
Java Spring environment in vs Code
Write test code in Spring Boot
Solve AtCoder Beginner Contest 153 in java
Implement simple login function in Rails
Implement tagging function in form object
Implement reCAPTCHA v3 in Java / Spring
Implement Material Design View in Kotlin
Implement PHP implode function in Java
Implement a gRPC client in Ruby
Solve AtCoder Beginner Contest 175 in java
Implement REST API in Spring Boot
Solve AtCoder Beginner Contest 160 in java
Implement a contact form in Rails
Implement Spring Boot application in Gradle
Try to implement Yubaba in Java
Solve AtCoder Beginner Contest 152 in java
Implement CSV download function in Rails
Solve AtCoder Beginner Contest 156 in java
1 Implement simple lexical analysis in Java
Bad habits pointed out in code reviews when Java was a beginner