网站开发的原理,何炅做的代言网站,从事网站建设需要什么资质,WordPress个人网站搭建文章目录 一、前言二、UIViewController三、UINavigationController四、UITabBarController五、UIPageViewController六、拓展阅读 一、前言
iOS 界面开发最重要的首属ViewController和View#xff0c;ViewController是View的控制器#xff0c;也就是一般的页面#xff0c;… 文章目录 一、前言二、UIViewController三、UINavigationController四、UITabBarController五、UIPageViewController六、拓展阅读 一、前言
iOS 界面开发最重要的首属ViewController和ViewViewController是View的控制器也就是一般的页面用来管理页面的生命周期它相当于安卓里的Activity两者很像但又有一些差异。
ViewController的特点是它有好几种。一种最基本的UIViewController和另外三种容器UINavigationController、UITabBarController、UIPageViewController。
所谓容器就是它们本身不能单独用来显示必须在里面放一个或几个UIViewController。
不同容器有不同的页面管理方式和展示效果
UINavigationController 用于导航栏管理页面UITabBarController 用于底部tab管理页面UIPageViewController 用于切换器管理页面
容器还可以嵌套比如把UITabBarController放进UINavigationController里面这样在tab页面里可以用启动导航栏样式的二级子页面。
二、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 .darkGraypresent(vc, animated: true)}objc func presentNC() {let vc ViewController()vc.view.backgroundColor .graylet nc UINavigationController(rootViewController: vc)present(nc, animated: true)}objc func presentTC() {let tc MyTabbarController()tc.view.backgroundColor .bluelet nc UINavigationController(rootViewController: tc)present(nc, animated: true)}objc func presentPC() {let pc MyPageViewController()pc.view.backgroundColor .redlet nc UINavigationController(rootViewController: pc)present(nc, animated: true)}objc func pushVC() {let vc ViewController()vc.view.backgroundColor .purpleif let nc navigationController {nc.pushViewController(vc, animated: true)} else {print(navigationController nil!)}}
}三、UINavigationController
这是最常用的页面导航方式顶部展示导航栏有标题、返回按钮。
使用pushViewController方法展示展示时从右往左出现可以用右滑手势关闭也可以多次启动叠加多个页面。
注意⚠️UINavigationController用来管理一组UIViewController这些UIViewController共用一个导航栏。
一般来说UINavigationController能很好地控制导航栏上面的元素显示和转场效果。
如果需要定制导航栏元素尽量修改UIViewController的导航栏不要直接修改UINavigationController的导航栏。 四、UITabBarController
这个一般用来做主页面的展示下面配置多个tab用于切换页面。 示例代码如下
class MyTabbarController: UITabBarController {init() {super.init(nibName: nil, bundle: nil)self.tabBar.backgroundColor .graylet vc1 ViewController()vc1.tabBarItem.image UIImage(named: diamond)vc1.tabBarItem.title tab1vc1.view.backgroundColor .redlet vc2 ViewController()vc2.tabBarItem.image UIImage(named: diamond)vc2.tabBarItem.title tab2vc2.view.backgroundColor .bluelet vc3 ViewController()vc3.tabBarItem.image UIImage(named: diamond)vc3.tabBarItem.title tab3vc3.view.backgroundColor .purpleself.viewControllers [vc1,vc2,vc3,]}required init?(coder: NSCoder) {fatalError(init(coder:) has not been implemented)}
}五、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 selflet vc1 ViewController()vc1.view.backgroundColor .redlet vc2 ViewController()vc2.view.backgroundColor .bluelet vc3 ViewController()vc3.view.backgroundColor .purplelet vc4 ViewController()vc4.view.backgroundColor .grayvcs [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) - 1if i 0 {return nil}return vcs[i]}func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) - UIViewController? {let i (vcs.firstIndex(of: viewController as! ViewController) ?? 0) 1if i vcs.count {return nil}return vcs[i]}
}六、拓展阅读
《iOS开发进阶十viewController生命周期讲解》 文章转载自: http://www.morning.wjpsn.cn.gov.cn.wjpsn.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.rrgqq.cn.gov.cn.rrgqq.cn http://www.morning.bpmnl.cn.gov.cn.bpmnl.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.tgfjm.cn.gov.cn.tgfjm.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.dwmtk.cn.gov.cn.dwmtk.cn http://www.morning.rgnp.cn.gov.cn.rgnp.cn http://www.morning.mswkd.cn.gov.cn.mswkd.cn http://www.morning.mnqg.cn.gov.cn.mnqg.cn http://www.morning.kpwdt.cn.gov.cn.kpwdt.cn http://www.morning.ttdbr.cn.gov.cn.ttdbr.cn http://www.morning.clqpj.cn.gov.cn.clqpj.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.rui931.cn.gov.cn.rui931.cn http://www.morning.rwbh.cn.gov.cn.rwbh.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.tymwx.cn.gov.cn.tymwx.cn http://www.morning.cgbgc.cn.gov.cn.cgbgc.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.sgmis.com.gov.cn.sgmis.com http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.mplb.cn.gov.cn.mplb.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.jjzxn.cn.gov.cn.jjzxn.cn http://www.morning.mtbth.cn.gov.cn.mtbth.cn http://www.morning.rrms.cn.gov.cn.rrms.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.yhpq.cn.gov.cn.yhpq.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.rsszk.cn.gov.cn.rsszk.cn http://www.morning.qwmsq.cn.gov.cn.qwmsq.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.tmpsc.cn.gov.cn.tmpsc.cn http://www.morning.bklhx.cn.gov.cn.bklhx.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.qytpt.cn.gov.cn.qytpt.cn http://www.morning.lxmks.cn.gov.cn.lxmks.cn http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.pmtky.cn.gov.cn.pmtky.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.qbwbs.cn.gov.cn.qbwbs.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.pndhh.cn.gov.cn.pndhh.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.kqwsy.cn.gov.cn.kqwsy.cn http://www.morning.brsgw.cn.gov.cn.brsgw.cn http://www.morning.jynzb.cn.gov.cn.jynzb.cn http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.tkchm.cn.gov.cn.tkchm.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.sgbss.cn.gov.cn.sgbss.cn http://www.morning.drnjn.cn.gov.cn.drnjn.cn http://www.morning.ntyks.cn.gov.cn.ntyks.cn