You can use navigation controllers or custom view controllers as the root view controller for a tab. If the root view controller is a navigation controller, the tab bar controller makes further adjustments to the size of the displayed navigation content so that it doesn’t overlap the tab bar.
Tab의 RootViewController 는 navigationController 또는 CustomViewController를 사용할 수 있습니다. RootViewController가 navigationController인 경우 TabBarController는 표시되는 콘텐츠의 크기를 TabBar와 겹치지 않게 추가로 조정합니다.
import UIKit
class TabController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.setupTabs()
self.tabBar.isTranslucent = true
self.tabBar.backgroundColor = .clear
}
private func setupTabs(){
let today = self.createNav(with: "투데이", and: UIImage(systemName: "doc.text.image"), vc: TodayViewController())
let game = self.createNav(with: "게임", and: UIImage(systemName: "gamecontroller"), vc: GameViewController())
let app = self.createNav(with: "앱", and: UIImage(systemName: "square.stack.3d.up.fill"), vc: AppViewController())
let search = self.createNav(with: "검색", and: UIImage(systemName: "magnifyingglass"), vc: SearchViewController())
self.setViewControllers([today,game,app,search], animated: true)
}
private func createNav(with title: String, and image: UIImage?, vc: UIViewController) -> UINavigationController {
vc.view.backgroundColor = .systemBackground
let nav = UINavigationController(rootViewController: vc)
nav.setupNavBar()
nav.tabBarItem.title = title
nav.tabBarItem.image = image
nav.viewControllers.first?.navigationItem.title = title
nav.navigationBar.prefersLargeTitles = true
nav.navigationBar.isTranslucent = true
nav.navigationItem.largeTitleDisplayMode = .always
nav.viewControllers.first?.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "person.crop.circle"), style: .plain, target: self, action: nil)
return nav
}
}
extension UINavigationController {
func setupNavBar(){
let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundColor = .systemBackground
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.label]
appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.label]
self.navigationBar.standardAppearance = appearance
self.navigationBar.scrollEdgeAppearance = appearance
self.navigationBar.compactAppearance = appearance
}
}
self.setViewControllers([today,game,app,search], animated: true)
이 부분이 제일 중요하다 정도만 기억하면 좋을꺼 같다. func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.rootViewController = ViewController() // 원하는 시작 ViewController
window?.makeKeyAndVisible()
}
The backdrop for your app’s user interface and the object that dispatches events to your views.
앱 UI 의 배경막 역할을 하고 뷰에 이벤트를 전달하는 오브젝트