-
코드로 TabBar Controller 연결하기iOS/Swift 2022. 1. 11. 22:37
안녕하세요!! 이번엔 코드로 TabBar Controller를 연결하는 방법을 정리했습니다.
이번 글도 노션에서 옮겨와 말이 짧습니다!! 양해해주세용😽
코드로 Navigation Controller 연결하기
안녕하세요!! 코드로 Navigaiton Controller를 연결하는 방법을 정리해보았습니다. 노션에서 작성한 글을 옮겨 말이 짧습니다. 양해해주세용😽 스토리보드에서는 간편하게 ViewController를 누르고 Editor
makemakeway.tistory.com
위에서 했던 것과 같이, 패키지파일 → Target → Info에서 스토리보드 관련 요소들을 삭제한다.
그리고, New file → Cocoa Touch Class에서 UITabBarController를 하나 만들어준다.
class TabBarController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() setUpTabBar() } private func setUpTabBar() { let homeViewController = UINavigationController(rootViewController: ViewController()) homeViewController.tabBarItem.image = UIImage(systemName: "house") // TabBar Item 의 이미지 homeViewController.tabBarItem.title = "홈" // TabBar Item 의 이름 let secondViewController = UINavigationController(rootViewController: SecondViewController()) secondViewController.tabBarItem.image = UIImage(systemName: "heart") secondViewController.tabBarItem.title = "찜한가게" let thirdViewController = UINavigationController(rootViewController: ThirdViewController()) thirdViewController.tabBarItem.image = UIImage(systemName: "doc.text") thirdViewController.tabBarItem.title = "주문내역" let fourthViewController = UINavigationController(rootViewController: FourthViewController()) fourthViewController.tabBarItem.image = UIImage(systemName: "person") fourthViewController.tabBarItem.title = "my배민" viewControllers = [homeViewController, secondViewController, thirdViewController, fourthViewController] } }
네비게이션 컨트롤러를 사용할 것이라면 위 코드처럼 원하는 뷰 컨트롤러를 UINavigationController의 rootViewController로 설정하면 된다.
tabBarItem.image에는 탭바에 들어갈 이미지, tabBarItem.title에는 탭바 이미지 아래에 들어갈 텍스트를 넣어줄 수 있다.
그리고 SceneDelegate 파일에서 코드를 작성한다.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) else { return } self.window = UIWindow(windowScene: windowScene) let tabbar = TabBarController() self.window?.rootViewController = tabbar self.window?.makeKeyAndVisible() }
코드를 작성한 뒤 시뮬레이터를 작동하면, 정상적으로 적용된 것을 볼 수 있다!!
'iOS > Swift' 카테고리의 다른 글
UISearchBar에서 사용자가 입력을 멈췄을 때 자동으로 검색하기(실시간 검색) (0) 2022.01.11 특정 모서리에만 CornerRadius 적용하기 (0) 2022.01.11 UITableViewCell 동적 높이 할당(UITableView Dynamic height) (0) 2022.01.11 UserDefaults에서 커스텀 객체 사용하기 (0) 2022.01.11 코드로 Navigation Controller 연결하기 (0) 2022.01.11