iOS各種ViewController控制器使用示例完整介紹
正文
iOS 界面開(kāi)發(fā)最重要的是ViewController和View,ViewController是View的控制器,也就是一般的頁(yè)面,用來(lái)管理頁(yè)面的生命周期(它相當(dāng)于安卓里的Activity,兩者很像,又有一些差異)。
ViewController的特點(diǎn)是它有好幾種。一種最基本的UIViewController,和另外三種容器:UINavigationController、UITabBarController、UIPageViewController。
所謂容器,就是它們本身不能單獨(dú)用來(lái)顯示,必須在里面放一個(gè)或幾個(gè)UIViewController。
不同容器有不同的頁(yè)面管理方式和展示效果:
- UINavigationController 用導(dǎo)航欄管理頁(yè)面
- UITabBarController 用底部tab管理頁(yè)面
- UIPageViewController 用切換器管理頁(yè)面
容器還可以嵌套,比如把UITabBarController放進(jìn)UINavigationController里面,這樣在tab頁(yè)面里,可以用啟動(dòng)導(dǎo)航欄樣式的二級(jí)子頁(yè)面。
1 UIViewController
這是最簡(jiǎn)單的頁(yè)面,沒(méi)有導(dǎo)航欄。
使用present方法展示,展示時(shí)從底部彈起,可以用下滑手勢(shì)關(guān)閉,也可以多次啟動(dòng)疊加多個(gè)頁(yè)面。

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
title = "\(self.hash)"
var label = UIButton(frame: CGRect(x: 10, y: 100, width: 300, height: 100))
label.setTitle("present ViewController", for: .normal)
view.addSubview(label)
label.addTarget(self, action: #selector(presentVC), for: .touchUpInside)
label = UIButton(frame: CGRect(x: 10, y: 200, width: 300, height: 100))
label.setTitle("present NavigationController", for: .normal)
view.addSubview(label)
label.addTarget(self, action: #selector(presentNC), for: .touchUpInside)
label = UIButton(frame: CGRect(x: 10, y: 300, width: 300, height: 100))
label.setTitle("push ViewController", for: .normal)
view.addSubview(label)
label.addTarget(self, action: #selector(pushVC), for: .touchUpInside)
label = UIButton(frame: CGRect(x: 10, y: 400, width: 300, height: 100))
label.setTitle("present TabbarController", for: .normal)
view.addSubview(label)
label.addTarget(self, action: #selector(presentTC), for: .touchUpInside)
label = UIButton(frame: CGRect(x: 10, y: 500, width: 300, height: 100))
label.setTitle("present PageViewController", for: .normal)
view.addSubview(label)
label.addTarget(self, action: #selector(presentPC), for: .touchUpInside)
}
@objc func presentVC() {
let vc = ViewController()
vc.view.backgroundColor = .darkGray
present(vc, animated: true)
}
@objc func presentNC() {
let vc = ViewController()
vc.view.backgroundColor = .gray
let nc = UINavigationController(rootViewController: vc)
present(nc, animated: true)
}
@objc func presentTC() {
let tc = MyTabbarController()
tc.view.backgroundColor = .blue
let nc = UINavigationController(rootViewController: tc)
present(nc, animated: true)
}
@objc func presentPC() {
let pc = MyPageViewController()
pc.view.backgroundColor = .red
let nc = UINavigationController(rootViewController: pc)
present(nc, animated: true)
}
@objc func pushVC() {
let vc = ViewController()
vc.view.backgroundColor = .purple
if let nc = navigationController {
nc.pushViewController(vc, animated: true)
} else {
print("navigationController nil!")
}
}
} 2 UINavigationController
這是最常用的頁(yè)面導(dǎo)航方式,頂部展示導(dǎo)航欄,有標(biāo)題、返回按鈕。
使用pushViewController方法展示,展示時(shí)從右往左出現(xiàn),可以用右滑手勢(shì)關(guān)閉,也可以多次啟動(dòng)疊加多個(gè)頁(yè)面。
注意:
UINavigationController用來(lái)管理一組UIViewController,這些UIViewController共用一個(gè)導(dǎo)航欄。
一般來(lái)說(shuō),UINavigationController能很好地控制導(dǎo)航欄上面的元素顯示和轉(zhuǎn)場(chǎng)效果。
如果需要定制導(dǎo)航欄元素,盡量修改UIViewController的導(dǎo)航欄,不要直接修改UINavigationController的導(dǎo)航欄。

3 UITabBarController
這個(gè)一般用來(lái)做主頁(yè)面的展示,下面配置多個(gè)tab,用來(lái)切換頁(yè)面。

class MyTabbarController: UITabBarController {
init() {
super.init(nibName: nil, bundle: nil)
self.tabBar.backgroundColor = .gray
let vc1 = ViewController()
vc1.tabBarItem.image = UIImage(named: "diamond")
vc1.tabBarItem.title = "tab1"
vc1.view.backgroundColor = .red
let vc2 = ViewController()
vc2.tabBarItem.image = UIImage(named: "diamond")
vc2.tabBarItem.title = "tab2"
vc2.view.backgroundColor = .blue
let vc3 = ViewController()
vc3.tabBarItem.image = UIImage(named: "diamond")
vc3.tabBarItem.title = "tab3"
vc3.view.backgroundColor = .purple
self.viewControllers = [
vc1,
vc2,
vc3,
]
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}4 UIPageViewController
這個(gè)用來(lái)做翻頁(yè)的頁(yè)面,比如電子書(shū)或者廣告banner??梢耘渲米笥一蛏舷路g,翻頁(yè)效果可以配置滾動(dòng)或者模擬翻書(shū)。
用viewControllerBefore和viewControllerAfter回調(diào)方法控制頁(yè)面切換。viewControllerBefore方法是讓我們給它提供當(dāng)前頁(yè)面的前一個(gè)頁(yè)面,viewControllerAfter方法是讓我們給它提供當(dāng)前頁(yè)面的后一個(gè)頁(yè)面。
注意:
UIPageViewController有預(yù)加載機(jī)制,它會(huì)提前加載當(dāng)前頁(yè)面的前后頁(yè)面。
但是它沒(méi)有實(shí)現(xiàn)頁(yè)面緩存機(jī)制,需要我們?cè)谕獠孔鼍彺妗?/p>
如果頁(yè)面非常多,但又是同一個(gè)類(lèi)的實(shí)例,那么一般創(chuàng)建三個(gè)實(shí)例就夠了,然后在viewControllerBefore和viewControllerAfter方法里循環(huán)使用這三個(gè)。


class MyPageViewController: UIPageViewController, UIPageViewControllerDataSource {
lazy var vcs = [
ViewController(),
ViewController(),
ViewController(),
ViewController(),
ViewController(),
]
init() {
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal)
self.dataSource = self
let vc1 = ViewController()
vc1.view.backgroundColor = .red
let vc2 = ViewController()
vc2.view.backgroundColor = .blue
let vc3 = ViewController()
vc3.view.backgroundColor = .purple
let vc4 = ViewController()
vc4.view.backgroundColor = .gray
vcs = [vc1,vc2,vc3,vc4
]
self.setViewControllers([vcs[0]], direction: .forward, animated: false)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let i = (vcs.firstIndex(of: viewController as! ViewController) ?? 0) - 1
if i < 0 {
return nil
}
return vcs[i]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let i = (vcs.firstIndex(of: viewController as! ViewController) ?? 0) + 1
if i >= vcs.count {
return nil
}
return vcs[i]
}
}以上就是iOS各種ViewController控制器使用示例完整介紹的詳細(xì)內(nèi)容,更多關(guān)于iOS ViewController控制器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解iOS應(yīng)用開(kāi)發(fā)中使用設(shè)計(jì)模式中的抽象工廠模式
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中使用設(shè)計(jì)模式中的抽象工廠模式,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
解決Xcode 8構(gòu)建版本iTunes Connect獲取不到應(yīng)用程序狀態(tài)的辦法
這篇文章主要介紹了關(guān)于解決Xcode 8構(gòu)建版本iTunes Connect獲取不到應(yīng)用程序狀態(tài)的辦法,需要的朋友可以參考下2017-03-03
實(shí)例解析iOS開(kāi)發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用
這篇文章主要介紹了iOS開(kāi)發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10
IOS開(kāi)發(fā)之路--C語(yǔ)言預(yù)處理
由于預(yù)處理指令是在編譯之前就進(jìn)行了,因此很多時(shí)候它要比在程序運(yùn)行時(shí)進(jìn)行操作效率高。在C語(yǔ)言中包括三類(lèi)預(yù)處理指令,今天將一一介紹:宏定義、條件編譯、文件包含2014-08-08
iOS實(shí)現(xiàn)數(shù)字倍數(shù)動(dòng)畫(huà)效果
在iOS開(kāi)發(fā)中,制作動(dòng)畫(huà)效果是最讓開(kāi)發(fā)者享受的環(huán)節(jié)之一,下面這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)數(shù)字倍數(shù)動(dòng)畫(huà)效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02

