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

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
這是最常用的頁面導(dǎo)航方式,頂部展示導(dǎo)航欄,有標題、返回按鈕。
使用pushViewController方法展示,展示時從右往左出現(xiàn),可以用右滑手勢關(guān)閉,也可以多次啟動疊加多個頁面。
注意:
UINavigationController用來管理一組UIViewController,這些UIViewController共用一個導(dǎo)航欄。
一般來說,UINavigationController能很好地控制導(dǎo)航欄上面的元素顯示和轉(zhuǎn)場效果。
如果需要定制導(dǎo)航欄元素,盡量修改UIViewController的導(dǎo)航欄,不要直接修改UINavigationController的導(dǎo)航欄。

3 UITabBarController
這個一般用來做主頁面的展示,下面配置多個tab,用來切換頁面。

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
這個用來做翻頁的頁面,比如電子書或者廣告banner??梢耘渲米笥一蛏舷路g,翻頁效果可以配置滾動或者模擬翻書。
用viewControllerBefore和viewControllerAfter回調(diào)方法控制頁面切換。viewControllerBefore方法是讓我們給它提供當前頁面的前一個頁面,viewControllerAfter方法是讓我們給它提供當前頁面的后一個頁面。
注意:
UIPageViewController有預(yù)加載機制,它會提前加載當前頁面的前后頁面。
但是它沒有實現(xiàn)頁面緩存機制,需要我們在外部做緩存。
如果頁面非常多,但又是同一個類的實例,那么一般創(chuàng)建三個實例就夠了,然后在viewControllerBefore和viewControllerAfter方法里循環(huán)使用這三個。


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控制器使用示例完整介紹的詳細內(nèi)容,更多關(guān)于iOS ViewController控制器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解iOS應(yīng)用開發(fā)中使用設(shè)計模式中的抽象工廠模式
這篇文章主要介紹了iOS應(yīng)用開發(fā)中使用設(shè)計模式中的抽象工廠模式,示例代碼為傳統(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
實例解析iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用
這篇文章主要介紹了iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10

