欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS各種ViewController控制器使用示例完整介紹

 更新時(shí)間:2023年07月05日 10:43:49   作者:rome753  
這篇文章主要為大家介紹了iOS各種ViewController控制器使用示例完整介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

iOS 界面開發(fā)最重要的是ViewController和View,ViewController是View的控制器,也就是一般的頁面,用來管理頁面的生命周期(它相當(dāng)于安卓里的Activity,兩者很像,又有一些差異)。

ViewController的特點(diǎn)是它有好幾種。一種最基本的UIViewController,和另外三種容器:UINavigationController、UITabBarController、UIPageViewController。

所謂容器,就是它們本身不能單獨(dú)用來顯示,必須在里面放一個(gè)或幾個(gè)UIViewController。

不同容器有不同的頁面管理方式和展示效果:

  • UINavigationController 用導(dǎo)航欄管理頁面
  • UITabBarController 用底部tab管理頁面
  • UIPageViewController 用切換器管理頁面

容器還可以嵌套,比如把UITabBarController放進(jìn)UINavigationController里面,這樣在tab頁面里,可以用啟動(dòng)導(dǎo)航欄樣式的二級(jí)子頁面。

1 UIViewController

這是最簡(jiǎn)單的頁面,沒有導(dǎo)航欄。

使用present方法展示,展示時(shí)從底部彈起,可以用下滑手勢(shì)關(guān)閉,也可以多次啟動(dòng)疊加多個(gè)頁面。

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)航欄,有標(biāo)題、返回按鈕。

使用pushViewController方法展示,展示時(shí)從右往左出現(xiàn),可以用右滑手勢(shì)關(guān)閉,也可以多次啟動(dòng)疊加多個(gè)頁面。

注意:

UINavigationController用來管理一組UIViewController,這些UIViewController共用一個(gè)導(dǎo)航欄。

一般來說,UINavigationController能很好地控制導(dǎo)航欄上面的元素顯示和轉(zhuǎn)場(chǎng)效果。

如果需要定制導(dǎo)航欄元素,盡量修改UIViewController的導(dǎo)航欄,不要直接修改UINavigationController的導(dǎo)航欄。

3 UITabBarController

這個(gè)一般用來做主頁面的展示,下面配置多個(gè)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

這個(gè)用來做翻頁的頁面,比如電子書或者廣告banner。可以配置左右或上下翻譯,翻頁效果可以配置滾動(dòng)或者模擬翻書。

用viewControllerBefore和viewControllerAfter回調(diào)方法控制頁面切換。viewControllerBefore方法是讓我們給它提供當(dāng)前頁面的前一個(gè)頁面,viewControllerAfter方法是讓我們給它提供當(dāng)前頁面的后一個(gè)頁面。

注意:

UIPageViewController有預(yù)加載機(jī)制,它會(huì)提前加載當(dāng)前頁面的前后頁面。

但是它沒有實(shí)現(xiàn)頁面緩存機(jī)制,需要我們?cè)谕獠孔鼍彺妗?/p>

如果頁面非常多,但又是同一個(gè)類的實(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)文章

最新評(píng)論