swift中UIViewController的使用

前端之家收集整理的这篇文章主要介绍了swift中UIViewController的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

UIViewController视图控制器在iOS研发中不可或缺,基本上每一个页都的研发都会使用到。

在使用过程中,主要使用了以下几个方面。

1、视图控制器的属性设置。如背景颜色,适配,视图控制器数组属性

2、视图控制器的生命周期的控制

3、视图控制器间的转场present,或push,以及相对应的dismiss,或pop

……

  1. // MARK: - 适配
  2. func autoSize()
  3. {
  4. if self.respondsToSelector(Selector("edgesForExtendedLayout"))
  5. {
  6. self.edgesForExtendedLayout = UIRectEdge.None
  7. }
  8. if self.respondsToSelector(Selector("extendedLayoutIncludesOpaqueBars"))
  9. {
  10. self.extendedLayoutIncludesOpaqueBars = false
  11. }
  12. if self.respondsToSelector(Selector("automaticallyAdjustsScrollViewInsets"))
  13. {
  14. self.automaticallyAdjustsScrollViewInsets = false
  15. }
  16. }
  1. // MARK: - 根视图控制器
  2. var isRootViewController:Bool
  3. {
  4. get
  5. {
  6. if self.navigationController!.viewControllers.first!.isEqual(self)
  7. {
  8. return true
  9. }
  10. return false
  11. }
  12. }
  1. // MARK: - 视图控制器索引下标值
  2. var indexViewController:Int
  3. {
  4. get
  5. {
  6. let indexVC = self.navigationController!.viewControllers.indexOf(self)!
  7. return indexVC
  8. }
  9. }
  1. // MARK: - 返回上层视图控制器
  2. func backPrevIoUsController()
  3. {
  4. if self.isRootViewController
  5. {
  6. self.dismissViewControllerAnimated(true,completion: nil)
  7. }
  8. else
  9. {
  10. if (self.presentedViewController != nil)
  11. {
  12. self.dismissViewControllerAnimated(true,completion: nil)
  13. }
  14. else
  15. {
  16. self.navigationController!.popViewControllerAnimated(true)
  17. }
  18. }
  19. }
  1. override func loadView() {
  2. super.loadView()
  3. // 视图控制器背景颜色
  4. self.view.backgroundColor = UIColor.whiteColor()
  5. }
  1. // present视图控制器
  2. let nextVC = PresentViewController()
  3. let nextNav = UINavigationController(rootViewController: nextVC)
  4. /*
  5. 视图控制器翻转效果
  6. 由下向上推出(默认模式) CoverVertical
  7. 水平翻转 FlipHorizontal
  8. 淡入淡出 CrossDissolve
  9. 翻页效果 PartialCurl
  10. 注意:如果有导航视图控制器时,翻转效果设置在导航视图控制器;没有时则设置在视图控制器。
  11. */
  12. nextNav.modalTransitionStyle = UIModalTransitionStyle.PartialCurl
  13. self.presentViewController(nextNav,animated: true,completion: nil)
  1. // 返回上一个视图控制器
  2. self.dismissViewControllerAnimated(true,completion: nil)
  1. // push视图控制器
  2. let nextVC = PopViewController()
  3. // self.navigationController!.pushViewController(nextVC,animated: true)
  4. // 转场动画1
  5. UIView.beginAnimations(nil,context: nil)
  6. UIView.setAnimationCurve(UIViewAnimationCurve.EaseInOut)
  7. UIView.setAnimationDuration(0.6)
  8. self.navigationController!.pushViewController(nextVC,animated: true)
  9. UIView.setAnimationTransition(UIViewAnimationTransition.CurlUp,forView: self.navigationController!.view,cache: false)
  10. UIView.commitAnimations()
  1. // 转场动画2
  2. let animation = CATransition()
  3. animation.duration = 0.6
  4. animation.type = kCATransitionReveal
  5. animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaSEOut)
  6. animation.subtype = kCATransitionFromBottom
  7. self.navigationController!.pushViewController(nextVC,animated: true)
  8. self.navigationController!.view.layer.addAnimation(animation,forKey: nil)
  1. // 返回上一个视图控制器
  2. self.navigationController!.popViewControllerAnimated(true)
  3. // 返回根视图控制器
  4. self.navigationController!.popToRootViewControllerAnimated(true)
  5. // 返回指定视图控制器
  6. let indexVC = self.navigationController!.viewControllers[2]
  7. self.navigationController!.popToViewController(indexVC,animated: true)

猜你在找的Swift相关文章