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
'iOS Swift > Study' 카테고리의 다른 글
[Swift] 책검색 App (5) - 책을 담았을 때 목록에서 확인하기 (feat. reloeadData 와 View Controller의 생명주기) (3) | 2024.05.07 |
---|---|
[Swift] 책검색 App (4) - 상세페이지로 이동, CoreData 로 책 담기 (2) | 2024.05.07 |
[Swift] 책검색 App (1) - 네트워크 통신 (2) | 2024.05.05 |
Swift 버튼 좌측에 이미지 넣기 시도하다가 포기하고 UIView 로 그린썰 푼다 (0) | 2024.05.04 |
Swift 버튼에 시스템 이미지 크기 바꾸기 (0) | 2024.05.04 |
댓글