๊ณต๋ถํ ๋ด์ฉ์ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
๋ฌธ์
ํญ ๋ฐ ์ปจํธ๋กค๋ฌ๋ฅผ ์ด์ฉํด ์ฌ๋ฌ ๊ฐ์ ๋ทฐ๋ฅผ ๋ฃ์ด๋ผ. ๋ทฐ์ ํน์ง์ ๋ง๊ฒ ํญ ๋ฐ ์์ด์ฝ๋ ์์ ํ๋ผ. ๋ํ์ฌ ์ด๋ฏธ์ง๋ทฐ์ ์ด๋ฏธ์ง๋ฅผ ํ ๋นํ๊ณ , ๋ฒํผ ํด๋ฆญ์ ํด๋น ๋ทฐ๋ก ์ ํ๋๋๋ก ๊ตฌํํ๋ผ.
๊ตฌํ ์์
- ๋ ์ด์์ ๊ตฌ์ฑ
- ํญ ๋ฐ ์ปจํธ๋กค๋ฌ ์คํ ๋ฆฌ ๋ณด๋์ ์ถ๊ฐ
- ํญ ๋ฐ์ ๋ทฐ ์ถ๊ฐ
- ํญ ๋ฐ ์์ด์ฝ ์์
- ๋ฒํผ์ผ๋ก ๋ทฐ ์ ํ ๊ตฌํ
์ต์ข ๊ฒฐ๊ณผ
1. ๋ ์ด์์ ๊ตฌ์ฑ (์ต์ข ์ ์ผ๋ก ๋ณผ ์ ์๋ ๋ ์ด์์)
ImageView์ ์ฌ์ง ํ ๋นํ๊ธฐ
ImageView๋ฅผ ํด๋ฆญํ ํ ์ค๋ฅธ์ชฝ ์ธ์คํํฐ ์์ญ์์ [Attributes inspector]์ image๋ฅผ ํตํด ์ํ๋ ์ด๋ฏธ์ง๋ฅผ ์ฝ๋ ์์ ์์ด ์ด๋ฏธ์ง ๋ทฐ์ ํ ๋นํ ์ ์๋ค.
2. ํญ ๋ฐ ์ปจํธ๋กค๋ฌ ์คํ ๋ฆฌ ๋ณด๋์ ์ถ๊ฐ
ํญ ๋ฐ ์ปจํธ๋กค๋ฌ Tap Bar Controller
์ฌ๋ฌ ๊ฐ์ ๋ทฐ์ ๋๋ถ์ด ๋ทฐ๋ฅผ ์ ํํด ์ด๋ํ ์ ์๊ฒ ํด์ฃผ๋ ์ปจํธ๋กค๋ฌ
@interface UITabBarController : UIViewController
A container view controller that manages a multiselection interface, where the selection determines which child view controller to display.
A container view controller that manages a multiselection interface, where the selection determines which child view controller to display.
๋ค์ค ์ ํ ์ธํฐํ์ด์ค๋ฅผ ๊ด๋ฆฌํ๋ ์ปจํ ์ด๋ ๋ทฐ ์ปจํธ๋กค๋ฌ์ด๋ค. ์ ํ์ ๋ฐ๋ผ ์ด๋ค ์์ ๋ทฐ ์ปจํธ๋กค๋ฌ๋ฅผ ๋ณด์ฌ์ค์ง ๊ฒฐ์ ํ๋ค.
@interface UITabBar : UIView
A control that displays one or more buttons in a tab bar for selecting between different subtasks, views, or modes in an app.
์ฑ์ ๋ค๋ฅธ ํ์ ์์ , ๋ทฐ ๋๋ ๋ชจ๋ ์ค ์ ํํ๊ธฐ ์ํด ํญ ๋ฐ์ ํ ๊ฐ ์ด์์ ๋ฒํผ์ ๋ณด์ฌ์ฃผ๋ ์ปจํธ๋กค์ด๋ค.
์คํ ๋ฆฌ๋ณด๋์ ์์ดํฐ ํ๋ฉด ์ ์ฒด๋ฅผ ๋๋๊ทธํ ํ ๋ฉ๋ด์์ [ Editor/ Embed in/ Tab Bar Controller ]๋ฅผ ์ ํํ๋ค.
3. ํญ ๋ฐ์ ๋ทฐ ์ถ๊ฐ
ViewController ๋ฅผ ์ถ๊ฐํ์ฌ ๋ทฐ๋ฅผ ์์ฑํ๊ฑฐ๋ ๊ธฐ์กด์ ์์ฑํ๋ ํ๋ก์ ํธ๋ฅผ ๋ณต์ฌํ์ฌ ๋ทฐ๋ฅผ ์์ฑํ ์ ์๋ค.
ViewControllew ๋ฅผ ์ถ๊ฐํ์ฌ ๋ทฐ๋ฅผ ์์ฑํ ๊ฒฝ์ฐ, UIVoewController ํด๋์ค๋ฅผ ์์๋ฐ๋ class๋ฅผ ํ๋ ์์ฑํ์ฌ ViewController์ Custom Class๋ก ์ง์ ํ๋ค.
๊ธฐ์กด ํ๋ก์ ํธ์์ ๋ทฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒฝ์ฐ, ํ๋ก์ ํธ ํ์ผ์ 'ViewController.swift' ํ์ผ์ ์ด๋ฆ ๋ณ๊ฒฝ ํ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ ํ๋ค. ๊ทธ ํ ํด๋น ํ๋ก์ ํธ Main.storyboard๋ฅผ ์ด์ด ViewController๊ฐ ๋ชจ๋ ํฌํจ๋๋๋ก ๋๋๊ทธํ ํ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ ํ๋ค. ์ด๋ ์๊น 'ViewController.swift' ํ์ผ ์ด๋ฆ์ ๋ณ๊ฒฝํ๊ธฐ ๋๋ฌธ์, ๋ณต์ฌํด์จ ๋ทฐ์ Custom Class๋ก ๋ณ๊ฒฝํ ์ด๋ฆ๋๋ก ์ค์ ํ๋ค. ๋ํ ํด๋น ํ์ผ ์ ํด๋์ค์ ์ด๋ฆ๋ ํ์ผ ์ด๋ฆ์ผ๋ก ๋ณ๊ฒฝํด์ผ ํ๋ค.
์ฌ์ฉํ ๋ทฐ๋ฅผ ๋ชจ๋ ์์ฑํ์ผ๋ฉด, 'Tab Bar Controller'๋ผ๊ณ ์ ํ์๋ ๋ทฐ์ ๋ง์ฐ์ค ์ค๋ฅธ์ชฝ ๋ฒํผ์ ํด๋ฆญํ ํ ๋ทฐ ํ๋์ฉ ์ฐ๊ฒฐํ๋ค.
4. ํญ ๋ฐ ์์ด์ฝ ์์
ํญ ๋ฐ์ ์๋ ์์ด์ฝ์ ํด๋ฆญํ ๋ค ์ค๋ฅธ์ชฝ ์ธ์คํํฐ ์์ญ์ [Attributes inspector]์์ System Item์ Custom์์ ๋ทฐ์ ๋ชฉ์ ์ ๋ง๊ฒ ๋ณ๊ฒฝํ๋ผ.
+ ์ค๋ฅธ์ชฝ ์ธ์คํํฐ ์์ญ์ [Attributes inspector]์์ Bar Item์ ํตํด ์์๋ก ์์ด์ฝ๊ณผ ํ์ดํ์ ์์ ํ ์ ์๋ค.
5. ๋ฒํผ์ผ๋ก ๋ทฐ ์ ํ ๊ตฌํ
๋ฒํผ์ ๋ ์ด์์์ ๋ฐฐ์นํ ํ ๋ฒํผ์ ์ํ ์ก์ ํจ์๋ฅผ ์์ฑํ๋ค.
๊ทธ ํ ๋ฒํผ ํด๋ฆญ ์ ์ํ๋ ๋ทฐ๋ก ์ ํํ ์ ์๋๋ก tabBarcontroller์ selectedIndex ํ๋กํผํฐ์ ํ๋ฉด ๋ฒํธ๋ฅผ ํ ๋นํ๋ค. ์ฌ๊ธฐ์ ํ๋ฉด ๋ฒํธ๋ ํญ ๋ฐ ์์ดํ ์ ์์์ด๋ค. ๋ฐฉํฅ์ ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก, 0๋ถํฐ ์์ํ๋ค.
selectedIndex ํ๋กํผํฐ์ ์ํ๋ ๋ทฐ์ ๋ฒํธ๋ฅผ ํ ๋นํ ๊ฒฝ์ฐ ํด๋น ๋ทฐ๋ก ๋ทฐ ์ ํ์ด ์ผ์ด๋๋ค.
var selectedIndex: Int { get set }
The index of the view controller associated with the currently selected tab item.
This property nominally represents an index into the array of the viewControllers property.
ํ์ฌ ์ ํ๋ ํญ ์์ดํ ๊ณผ ์ฐ๊ด๋ ๋ทฐ ์ปจํธ๋กค๋ฌ์ ๋ฒํธ
์ด ํ๋กํผํฐ๋ ๋ช ๋ชฉ์ ๋ทฐ ์ปจํธ๋กค๋ฌ ํ๋กํผํฐ์ ๋ฐฐ์ด ์ ๋ฒํธ๋ฅผ ๋ํ๋ธ๋ค.
import UIKit
class TabViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func btnMoveImageView(_ sender: UIButton) {
//์ด ์ซ์๋ ํญ ๋ฐ ์์ดํ
์์์ด๋ค. 0~
tabBarController?.selectedIndex = 1 //1-์ด๋ฏธ์ง ๋ทฐ ํ์ผ๋ก ์ด๋
}
@IBAction func btnMoveDatePickerView(_ sender: UIButton) {
//์ด ์ซ์๋ ํญ ๋ฐ ์์ดํ
์์์ด๋ค. 0~
tabBarController?.selectedIndex = 2 //2-๋ฐ์ดํธ ํผ์ปค ๋ทฐ ํญ์ผ๋ก ์ด๋
}
}
๊ณต๋ถ ์๋ฃ
Do It! ์ค์ํํธ๋ก ์์ดํฐ ์ฑ ๋ง๋ค๊ธฐ ๊ฐ์ 5ํ
SWIFT ๋ฌธ์