iOS各种ViewController控制器使用示例完整介绍

 更新时间:2023年07月05日 10:43:49   作者:rome753  
这篇文章主要为大家介绍了iOS各种ViewController控制器使用示例完整介绍,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

正文

iOS 界面开发最重要的是ViewController和View,ViewController是View的控制器,也就是一般的页面,用来管理页面的生命周期(它相当于安卓里的Activity,两者很像,又有一些差异)。

ViewController的特点是它有好几种。一种最基本的UIViewController,和另外三种容器:UINavigationController、UITabBarController、UIPageViewController。

所谓容器,就是它们本身不能单独用来显示,必须在里面放一个或几个UIViewController。

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

  • UINavigationController 用导航栏管理页面
  • UITabBarController 用底部tab管理页面
  • UIPageViewController 用切换器管理页面

容器还可以嵌套,比如把UITabBarController放进UINavigationController里面,这样在tab页面里,可以用启动导航栏样式的二级子页面。

1 UIViewController

这是最简单的页面,没有导航栏。

使用present方法展示,展示时从底部弹起,可以用下滑手势关闭,也可以多次启动叠加多个页面。

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

这是最常用的页面导航方式,顶部展示导航栏,有标题、返回按钮。

使用pushViewController方法展示,展示时从右往左出现,可以用右滑手势关闭,也可以多次启动叠加多个页面。

注意:

UINavigationController用来管理一组UIViewController,这些UIViewController共用一个导航栏。

一般来说,UINavigationController能很好地控制导航栏上面的元素显示和转场效果。

如果需要定制导航栏元素,尽量修改UIViewController的导航栏,不要直接修改UINavigationController的导航栏。

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。可以配置左右或上下翻译,翻页效果可以配置滚动或者模拟翻书。

用viewControllerBefore和viewControllerAfter回调方法控制页面切换。viewControllerBefore方法是让我们给它提供当前页面的前一个页面,viewControllerAfter方法是让我们给它提供当前页面的后一个页面。

注意:

UIPageViewController有预加载机制,它会提前加载当前页面的前后页面。

但是它没有实现页面缓存机制,需要我们在外部做缓存。

如果页面非常多,但又是同一个类的实例,那么一般创建三个实例就够了,然后在viewControllerBefore和viewControllerAfter方法里循环使用这三个。

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控制器使用示例完整介绍的详细内容,更多关于iOS ViewController控制器的资料请关注程序员之家其它相关文章!

相关文章

  • iOS开发中class和#import的区别介绍

    iOS开发中class和#import的区别介绍

    这篇文章主要介绍了iOS开发中class和#import的区别,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2018-02-02
  • iOS算法教程之分段截取常数示例

    iOS算法教程之分段截取常数示例

    这篇文章主要给大家介绍了关于iOS算法教程之分段截取常数的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2018-01-01
  • 2016年iOS公开可利用漏洞总结

    2016年iOS公开可利用漏洞总结

    本文总结了2016年比较严重的iOS漏洞(可用于远程代码执行或越狱),希望能够对大家移动安全方面的工作和研究带来一些帮助。
    2016-12-12
  • iOS获取验证码倒计时效果

    iOS获取验证码倒计时效果

    这篇文章主要为大家详细介绍了iOS获取验证码倒计时效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • iOS中block的定义与使用

    iOS中block的定义与使用

    苹果官方文档声明,block是objc对象。下面这篇文章主要给大家介绍了关于iOS中block的定义与使用,文中通过示例代码介绍的非常详细,对各位iOS开发者具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • ios开发中的容错处理示例详解

    ios开发中的容错处理示例详解

    最近发现还是有很多朋友在问类似解析时容错问题怎么解决,所以下面这篇文章主要给大家介绍了关于ios开发中的容错处理的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们一起来看看吧
    2018-05-05
  • 深入解析iOS应用开发中九宫格视图布局的相关计算方法

    深入解析iOS应用开发中九宫格视图布局的相关计算方法

    这篇文章主要介绍了iOS应用开发中九宫格视图布局的计算方法,包括item尺寸和坐标等一系列影像布局的数值相关计算的讲解,需要的朋友可以参考下
    2016-03-03
  • iOS实现类似格瓦拉电影的转场动画

    iOS实现类似格瓦拉电影的转场动画

    这篇文章主要给大家介绍了利用iOS如何实现类似格瓦拉电影的转场动画,文中给出了详细步骤实现代码,对大家的学习和理解很有帮助,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-11-11
  • iOS中多线程的入门使用教程(Swift)

    iOS中多线程的入门使用教程(Swift)

    这篇文章主要给大家介绍了关于iOS中多线程入门使用的相关资料,一个进程中可以开启多条线程,每条线程可以并行执行不同的任务,本文通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-11-11
  • iOS开发之image图片压缩及压缩成指定大小的两种方法

    iOS开发之image图片压缩及压缩成指定大小的两种方法

    这篇文章主要介绍了iOS开发之image图片压缩及压缩成指定大小的两种方法,需要的朋友可以参考下
    2017-11-11

最新评论

?


http://www.vxiaotou.com