메인이 되는 VC를 하나 준비한다.
ViewController
탭에 들어갈 VC들을 미리 준비한다.
FirstVC, SecondVC, ThirdVC 등…
tab 별 아이콘을 준비한다.
ViewController 의 상속 클래스인 UIViewController 를 삭제하고 UITabBarController 로 작성한다.
import UIKit
class ViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Create Tab one
let tabOne = FirstVC()
let tabOneBarItem = UITabBarItem(title: "Tab 1", image: UIImage(named: "Tab1_icon"), selectedImage: UIImage(named: "Tab1_icon"))
tabOne.tabBarItem = tabOneBarItem
// Create Tab two
let tabTwo = SecondVC()
let tabTwoBarItem2 = UITabBarItem(title: "Tab 2", image: UIImage(named: "Tab2_icon"), selectedImage: UIImage(named: "Tab2_icon"))
tabTwo.tabBarItem = tabTwoBarItem2
self.viewControllers = [tabOne, tabTwo]
self.delegate = self
}
}
appdelegate.swift 에서 앱의 window에 붙이기
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let tabVC = ViewController()
window?.rootViewController = tabVC
return true
}
}
탭 선택 처리하기 UITabBarControllerDelegate
ViewController 상속에 UITabBarControllerDelegate 추가
self.delegate = self
// MARK: - UITabBarControllerDelegate
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("didSelect item \(item.tag)")
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print(" \(tabBarController.selectedIndex)")
}
색상 설정
UITabBar.appearance().barTintColor = UIColor(red: 0, green: 0/255, blue: 205/255, alpha: 1)
// or
UITabBar.appearance().barTintColor = UIColor.blue
VC 에서 self.title = “title” 이라고 설정하면 TabBarItem의 텍스트가 자동으로 바뀐다.주의!
scenedelegate 에 바로 박아넣을 때
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let tabBarController = UITabBarController()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let homeVC = storyboard.instantiateViewController(identifier: "SB_NAV_HOME") as! UINavigationController
let settingsVC = storyboard.instantiateViewController(identifier: "SB_NAV_SETTINGS") as! UINavigationController
tabBarController.setViewControllers([homeVC, settingsVC], animated: false)
if let items = tabBarController.tabBar.items {
items[0].selectedImage = UIImage(systemName: "house.fill")
items[0].image = UIImage(systemName: "house")
items[0].title = "HOME".localized
items[1].selectedImage = UIImage(systemName: "gearshape.fill")
items[1].image = UIImage(systemName: "gearshape")
items[1].title = "SETTINGS".localized
}
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
// window?.overrideUserInterfaceStyle = .light
}