UITabBarController 사용하기
간단한 UITabBarController를 만들어 보았습니다.
UITabBarController에 setViewControllers를 사용하여 UIViewController를 넣어주면 UIViewController의 개수만큼 탭이 만들어지고 각 UIViewController에서 UITabBarItem을 생성해주면 해당 내용으로 이미지와 텍스트가 노출되게 됩니다.
UITabBarItem은 시스템에서 제공하는것을 사용하는 방법과 커스텀으로 텍스트와 이미지를 만드는 방법이 있습니다.
Tip)
UIViewController를 가지고 있는 Array를 map하여 UINavigationController의 rootViewController로 생성해주면 탭마다 UINavigationController를 쉽게 생성할 수 있습니다.
-> setViewControllers(tabs.map { UINavigationController(rootViewController: $0) }, animated: false)
아래 이미지와 소스코드를 비교해보시면 좀 더 이해하기 편합니다.
궁금하신점은 댓글로 달아주세요.
해피코딩 :)
Preview
Source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FirstTabBarViewController.swift | |
// UIKit component handling | |
// | |
// Created by Taehyeon Han on 2018. 8. 3.. | |
// Copyright © 2018년 calmone. All rights reserved. | |
// | |
import UIKit | |
class FirstTabBarViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
init() { | |
super.init(nibName: nil, bundle: nil) | |
// Set the background color of the view to Orange. | |
self.view.backgroundColor = .orange | |
// Define the tabBarItem's icon as Featured and Tag as 1. | |
self.tabBarItem = UITabBarItem(tabBarSystemItem: .featured, tag: 1) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RootTabBarViewController.swift | |
// UIKit component handling | |
// | |
// Created by Taehyeon Han on 2018. 8. 3.. | |
// Copyright © 2018년 calmone. All rights reserved. | |
// | |
import UIKit | |
class RootTabBarViewController: UITabBarController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
// Set the controller's title. | |
self.navigationItem.prompt = "UITabBarController" | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
init() { | |
super.init(nibName: nil, bundle: nil) | |
// Generate an instance of the ViewController to set on the Tab. | |
let firstTab: UIViewController = FirstTabBarViewController() | |
let secondTab: UIViewController = SecondTabBarViewController() | |
// Create an Array of Tables with Tabs as Elements. | |
let tabs = NSArray(objects: firstTab, secondTab) | |
// Set the ViewController. | |
self.setViewControllers(tabs as? [UIViewController], animated: false) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SecondTabBarViewController.swift | |
// UIKit component handling | |
// | |
// Created by Taehyeon Han on 2018. 8. 3.. | |
// Copyright © 2018년 calmone. All rights reserved. | |
// | |
import UIKit | |
class SecondTabBarViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
init() { | |
super.init(nibName: nil, bundle: nil) | |
// Set the background color of the view to Green. | |
self.view.backgroundColor = .green | |
// Define the tabBarItem's icon as Featured and Tag as 2. | |
self.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 2) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
} |
Github
https://github.com/calmone/iOS-UIKit-component
Reference
- UITabbarController
https://developer.apple.com/reference/uikit/uitabbarcontroller