navigation bar 를 생성하기 위해서는 UITabBarController
프로토콜을 채택해야 된다.
final class RMTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
navigation에 사용할 뷰컨 파일 생성
폴더 구조를 Controller/Core 로 그룹을 만들어 준다
new file -> Swift File 을 눌러서 프로젝트를 생성
import UIKit
final class RMCharacterViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
title = "Characters"
}
}
final
키워드를 붙여줌TabBar를 지정 할곳에서 UI를 작성할 함수를 viewDidLoad 함수 안에 만들어 준다
import UIKit
final class RMTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setUpTabs()
}
private func setUpTabs() {
}
}
생성한 뷰컨트롤 파일을 상수로 관리하고, UINavigationController
매서드로 정의 해준다
import UIKit
final class RMTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setUpTabs()
}
private func setUpTabs() {
let characterVC = RMCharacterViewController()
let locationVC = RMLocationViewController()
let episodesVC = RMEpisodeViewController()
let settingsVC = RMSettingViewController()
let nav1 = UINavigationController(rootViewController: characterVC)
let nav2 = UINavigationController(rootViewController: locationVC)
let nav3 = UINavigationController(rootViewController: episodesVC)
let nav4 = UINavigationController(rootViewController: settingsVC)
}
}
navigation 에 대한 타이틀이나 tab버튼에 대한 정의를 해준다
private func setUpTabs() {
let characterVC = RMCharacterViewController()
let locationVC = RMLocationViewController()
let episodesVC = RMEpisodeViewController()
let settingsVC = RMSettingViewController()
characterVC.navigationItem.largeTitleDisplayMode = .automatic
locationVC.navigationItem.largeTitleDisplayMode = .automatic
episodesVC.navigationItem.largeTitleDisplayMode = .automatic
settingsVC.navigationItem.largeTitleDisplayMode = .automatic
let nav1 = UINavigationController(rootViewController: characterVC)
let nav2 = UINavigationController(rootViewController: locationVC)
let nav3 = UINavigationController(rootViewController: episodesVC)
let nav4 = UINavigationController(rootViewController: settingsVC)
nav1.tabBarItem = UITabBarItem(title: "Characters",
image: UIImage(systemName: "person"),
tag: 1)
nav2.tabBarItem = UITabBarItem(title: "Locations",
image: UIImage(systemName: "globe"),
tag: 2)
nav3.tabBarItem = UITabBarItem(title: "Episodes",
image: UIImage(systemName: "tv"),
tag: 3)
nav4.tabBarItem = UITabBarItem(title: "Settings",
image: UIImage(systemName: "gear"),
tag: 4)
for nav in [nav1, nav2, nav3, nav4] {
nav.navigationBar.prefersLargeTitles = true
}
}
UITabBarItem
를 이용해서 설정 해준다setViewControllers(
[nav1, nav2, nav3, nav4],
animated: true
)
import UIKit
final class RMTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setUpTabs()
}
private func setUpTabs() {
let characterVC = RMCharacterViewController()
let locationVC = RMLocationViewController()
let episodesVC = RMEpisodeViewController()
let settingsVC = RMSettingViewController()
characterVC.navigationItem.largeTitleDisplayMode = .automatic
locationVC.navigationItem.largeTitleDisplayMode = .automatic
episodesVC.navigationItem.largeTitleDisplayMode = .automatic
settingsVC.navigationItem.largeTitleDisplayMode = .automatic
let nav1 = UINavigationController(rootViewController: characterVC)
let nav2 = UINavigationController(rootViewController: locationVC)
let nav3 = UINavigationController(rootViewController: episodesVC)
let nav4 = UINavigationController(rootViewController: settingsVC)
nav1.tabBarItem = UITabBarItem(title: "Characters",
image: UIImage(systemName: "person"),
tag: 1)
nav2.tabBarItem = UITabBarItem(title: "Locations",
image: UIImage(systemName: "globe"),
tag: 2)
nav3.tabBarItem = UITabBarItem(title: "Episodes",
image: UIImage(systemName: "tv"),
tag: 3)
nav4.tabBarItem = UITabBarItem(title: "Settings",
image: UIImage(systemName: "gear"),
tag: 4)
for nav in [nav1, nav2, nav3, nav4] {
nav.navigationBar.prefersLargeTitles = true
}
setViewControllers(
[nav1, nav2, nav3, nav4],
animated: true
)
}
}