본문 바로가기
iOS Swift/Study

[Swift] 책검색 App (2) - Tab Bar

by 야고이 2024. 5. 5.
728x90

이번 포스팅에서는 탭바를 만들어볼게요

 

탭바 설정은 SceneDelegate에서 할거예요

 

연결 할 뷰 객체 생성

let firstViewController = UINavigationController(rootViewController: MainViewController())
let SecondViewController = UINavigationController(rootViewController: SearchViewController())
let ThirdViewController = UINavigationController(rootViewController: LibraryViewController())

 

탭바컨트롤러 생성

let tabBarController = UITabBarController()
tabBarController.setViewControllers([firstViewController, SecondViewController, ThirdViewController], animated: true)

탭바컨트롤러에 넣을 뷰컨들을 배열로 담습니다

 

탭바에 들어갈 아이템 설정

if let items = tabBarController.tabBar.items {
    items[0].selectedImage = UIImage(systemName: "house")
    items[0].image = UIImage(systemName: "house")
    items[0].title = "Home"

    items[1].selectedImage = UIImage(systemName: "doc.text.magnifyingglass")
    items[1].image = UIImage(systemName: "doc.text.magnifyingglass")
    items[1].title = "Search"

    items[2].selectedImage = UIImage(systemName: "books.vertical")
    items[2].image = UIImage(systemName: "books.vertical")
    items[2].title = "Library"
}

 

탭바 꾸미기

UITabBarAppearance 는 탭바를 꾸밀 수 있는 클래스 이다

let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.backgroundColor = .white
tabBarAppearance.stackedLayoutAppearance.normal.iconColor = UIColor.ybgray
tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.ybgray]
tabBarAppearance.stackedLayoutAppearance.selected.iconColor = UIColor.ybyellow
tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.ybyellow]
UITabBar.appearance().standardAppearance = tabBarAppearance //탭바의 표준 모양으로 설정

선택한 탭와 선택 안한 탭의 색상 설정

 

rootViewController 설정

window?.rootViewController = tabBarController
window?.makeKeyAndVisible()

메인화면을 탭바컨트롤러로 설정하고 makeKeyAndVisible() 를 호출하여 창이 화면에 표시되게 한다

 

전채코드

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 firstViewController = UINavigationController(rootViewController: MainViewController())
        let SecondViewController = UINavigationController(rootViewController: SearchViewController())
        let ThirdViewController = UINavigationController(rootViewController: LibraryViewController())
        
        let tabBarController = UITabBarController()
        tabBarController.setViewControllers([firstViewController, SecondViewController, ThirdViewController], animated: true)
        
        if let items = tabBarController.tabBar.items {
            items[0].selectedImage = UIImage(systemName: "house")
            items[0].image = UIImage(systemName: "house")
            items[0].title = "Home"
            
            items[1].selectedImage = UIImage(systemName: "doc.text.magnifyingglass")
            items[1].image = UIImage(systemName: "doc.text.magnifyingglass")
            items[1].title = "Search"
            
            items[2].selectedImage = UIImage(systemName: "books.vertical")
            items[2].image = UIImage(systemName: "books.vertical")
            items[2].title = "Library"
        }
        
        let tabBarAppearance = UITabBarAppearance()
        tabBarAppearance.backgroundColor = .white
        tabBarAppearance.stackedLayoutAppearance.normal.iconColor = UIColor.ybgray
        tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.ybgray]
        tabBarAppearance.stackedLayoutAppearance.selected.iconColor = UIColor.ybyellow
        tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.ybyellow]
        UITabBar.appearance().standardAppearance = tabBarAppearance
        if #available(iOS 15.0, *) {
            UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
        }
        
        window?.rootViewController = tabBarController
        window?.makeKeyAndVisible()
        
    }

728x90

댓글