네비게이션컨트롤러 및 네비게이션바
https://developer.apple.com/documentation/uikit/uinavigationcontroller
UINavigationController | Apple Developer Documentation
A container view controller that defines a stack-based scheme for navigating hierarchical content.
developer.apple.com
https://developer.apple.com/documentation/uikit/uinavigationbar
UINavigationBar | Apple Developer Documentation
Navigational controls that display in a bar along the top of the screen, usually in conjunction with a navigation controller.
developer.apple.com
네비게이션 바를 코드로 구현
SceneDelegate.swift 파일에서 scene 함수에 아래 코드 작성
"UINavigationController" 인스턴스 생성
NaiviAndTab 폴더 내에 SecondViewController.swift 파일 생성
ViewController.swift 파일에 네비게이션 바 관련된 함수 구현
네비게이션 버튼을 눌렀을 때 동작할 함수 (didTapSecond) 구현 및
viewdidload 함수 설정
ViewController 전체 코드
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemYellow
configureNavigationBar()
}
@objc func didTapSecond() {
let SecondVC = SecondViewController()
navigationController?.pushViewController(SecondVC, animated: true)
}
private func configureNavigationBar() {
let appearance = UINavigationBarAppearance()
// appearance.configureWithOpaqueBackground() // 불투명으로
// appearance.backgroundColor = .brown // 색상설정
// appearance.configureWithTransparentBackground() // 투명으로
navigationController?.navigationBar.tintColor = .blue
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.compactAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
title = "Main"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(didTapSecond))
}
}