UIViewController视图控制器在iOS研发中不可或缺,基本上每一个页都的研发都会使用到。
在使用过程中,主要使用了以下几个方面。
1、视图控制器的属性设置。如背景颜色,适配,视图控制器数组属性等
2、视图控制器的生命周期的控制
3、视图控制器间的转场present,或push,以及相对应的dismiss,或pop
……
// MARK: - 适配 func autoSize() { if self.respondsToSelector(Selector("edgesForExtendedLayout")) { self.edgesForExtendedLayout = UIRectEdge.None } if self.respondsToSelector(Selector("extendedLayoutIncludesOpaqueBars")) { self.extendedLayoutIncludesOpaqueBars = false } if self.respondsToSelector(Selector("automaticallyAdjustsScrollViewInsets")) { self.automaticallyAdjustsScrollViewInsets = false } }
// MARK: - 根视图控制器 var isRootViewController:Bool { get { if self.navigationController!.viewControllers.first!.isEqual(self) { return true } return false } }
// MARK: - 视图控制器索引下标值 var indexViewController:Int { get { let indexVC = self.navigationController!.viewControllers.indexOf(self)! return indexVC } }
// MARK: - 返回上层视图控制器 func backPrevIoUsController() { if self.isRootViewController { self.dismissViewControllerAnimated(true,completion: nil) } else { if (self.presentedViewController != nil) { self.dismissViewControllerAnimated(true,completion: nil) } else { self.navigationController!.popViewControllerAnimated(true) } } }
override func loadView() { super.loadView() // 视图控制器背景颜色 self.view.backgroundColor = UIColor.whiteColor() }
// present视图控制器 let nextVC = PresentViewController() let nextNav = UINavigationController(rootViewController: nextVC) /* 视图控制器翻转效果 由下向上推出(默认模式) CoverVertical 水平翻转 FlipHorizontal 淡入淡出 CrossDissolve 翻页效果 PartialCurl 注意:如果有导航视图控制器时,翻转效果设置在导航视图控制器;没有时则设置在视图控制器。 */ nextNav.modalTransitionStyle = UIModalTransitionStyle.PartialCurl self.presentViewController(nextNav,animated: true,completion: nil)
// 返回上一个视图控制器 self.dismissViewControllerAnimated(true,completion: nil)
// push视图控制器 let nextVC = PopViewController() // self.navigationController!.pushViewController(nextVC,animated: true) // 转场动画1 UIView.beginAnimations(nil,context: nil) UIView.setAnimationCurve(UIViewAnimationCurve.EaseInOut) UIView.setAnimationDuration(0.6) self.navigationController!.pushViewController(nextVC,animated: true) UIView.setAnimationTransition(UIViewAnimationTransition.CurlUp,forView: self.navigationController!.view,cache: false) UIView.commitAnimations()
// 转场动画2 let animation = CATransition() animation.duration = 0.6 animation.type = kCATransitionReveal animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaSEOut) animation.subtype = kCATransitionFromBottom self.navigationController!.pushViewController(nextVC,animated: true) self.navigationController!.view.layer.addAnimation(animation,forKey: nil)
// 返回上一个视图控制器 self.navigationController!.popViewControllerAnimated(true) // 返回根视图控制器 self.navigationController!.popToRootViewControllerAnimated(true) // 返回指定视图控制器 let indexVC = self.navigationController!.viewControllers[2] self.navigationController!.popToViewController(indexVC,animated: true)