UIViewController类详解:
通过Nib文件初始化
StoryBoard相关
Unwindsegue的实现原理请参考相关文章
View相关
println("view = \(view)")
println("view is loaded = \(isViewLoaded())")
title = "ViewController"<pre name="code" class="objc">//如果不是nib文件初始化而来,初始化的时候需要调用这个方法初始化view,此方法不能主动调用,是系统调用的<pre name="code" class="objc">override func loadView() {
super.loadView()<pre name="code" class="objc">}//view初始化以后调用
模式跳转
- //设置模式跳转的类别,但是必须是目的Controller设置,不能是上级设置
- //CoverVertical, FlipHorizontal, CrossDissolve, PartialCurl四种类型
- viewController.modalTransitionStyle = .FlipHorizontal
- //设置模式展示样式,适合于iPad上
- viewController.modalPresentationStyle = .FullScreen
- //如果展示不是.FullScreen, 那么设置是不是捕获statusBar的样式,适合iPad
- viewController.modalPresentationCapturesStatusBarAppearance = true
- //判断在模式跳转时消失是否键盘
- viewController.disablesAutomaticKeyboardDismissal()
- presentViewController(viewController, animated: true) { () -> Void in
- //跳转到下个界面
- }
- dismissViewControllerAnimated(true , completion: { () -> Void in
- //回复模式跳转
- })
配置View的layout
- // layoutSubviews方法调用之前
- override func viewWillLayoutSubviews() {
- super.viewWillLayoutSubviews()
- }
- // layoutSubviews方法调用之后
- override func viewDidLayoutSubviews() {
- super.viewDidLayoutSubviews()
- }<pre name="code" class="objc">
updateViewConstraints()
- //延伸的方向--set which sides of your view can be extended to cover the whole screen.
- if self.respondsToSelector(Selector("edgesForExtendedLayout")) {
- self.edgesForExtendedLayout = .None
- }
- //Scrollview滚动时处于全屏,默认YES
- if self.respondsToSelector(Selector("automaticallyAdjustsScrollViewInsets")) {
- self.automaticallyAdjustsScrollViewInsets = true
- }
- //当statusbar是透明时,是否扩展至StatusBar,默认情况下是NO,且statusbar不是透明的
- if self.respondsToSelector(Selector("extendedLayoutIncludesOpaqueBars")) {
- self.extendedLayoutIncludesOpaqueBars = false
- }
- //控制view的大小UIPopoverController用的比较的广泛
- self.preferredContentSize = self.view.bounds.size
跳转相关
- isBeingPresented() //是否在展示
- isBeingDismissed() //是否在dismiss
- isMovingToParentViewController()
- isMovingFromParentViewController()
旋转相关
- //是否需要旋转
- override func shouldAutorotate() -> Bool {
- return true
- }
- //支持的方向
- override func supportedInterfaceOrientations() -> Int {
- return 2
- }
- //优先支持的方向
- override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
- return .Portrait
- }
自定义的ViewController Container
- //https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
- //addChildVIewController:调用这个方法指明一个视图控制器作为你的子视图。
- func addChildViewController(childController: UIViewController) {
- }
- //调用这个方法将一个视图控制器从你的子视图列表里移除。
- func removeFromParentViewController() {
- }
- //这是一个使用一个唯一可选的视图替换另一个视图的新方法,或者移动一个子视图到前台来。通过使用这个方法,这个视图控制器的生命周期信息会被正确地发送出去 func transitionFromViewController(fromViewController: UIViewController, toViewController: UIViewController, duration: NSTimeInterval, options: UIViewAnimationOptions, animations: () -> Void, completion: ((Bool) -> Void)?) {
- }
- //将要移到父Controller
- func willMoveToParentViewController(parent: UIViewController?) {
- }
- //已经移到父Controller
- func didMoveToParentViewController(parent: UIViewController?) {
- }
- //触发子ViewController的viewWillAppear
- func beginAppearanceTransition(isAppearing: Bool, animated: Bool) {
- }
- //触发childd的viewDidAppear这些方法
- func endAppearanceTransition() {
- }
- //child ViewController的作为状态栏
- func childViewControllerForStatusBarStyle() -> UIViewController? {
- return nil;
- }
- //child ViewController的状态栏是否隐藏设置状态栏
- func childViewControllerForStatusBarHidden() -> UIViewController? {
- return nil;
- }
恢复相关
- restorationIdentifier 恢复标示
- restorationClass 恢复的类
- override func encodeRestorableStateWithCoder(coder: NSCoder) {
- }
- override func decodeRestorableStateWithCoder(coder: NSCoder) {
- }
- applicationFinishedRestoringState() 恢复完成
获得其他的ViewController
- println("parentViewController=\(self.parentViewController)") //父类Controller
- println("presentedViewController=\(self.presentedViewController)") //Controller模式跳转到去Controller或父容器
- println("presentingViewController=\(self.presentingViewController)") //Controller模式跳转来自于Controller或父容器
- // self.navigationController
- // self.tabBarController
- // self.presentationController
- // self.splitViewController
- // self.popoverPresentationController
StatusBar相关
- //如果展示不是.FullScreen, 那么设置是不是捕获statusBar的样式,适合iPad
- viewController.modalPresentationCapturesStatusBarAppearance = true
- //child ViewController的作为状态栏
- func childViewControllerForStatusBarStyle() -> UIViewController? {
- return nil;
- }
- //child ViewController的状态栏是否隐藏设置状态栏
- func childViewControllerForStatusBarHidden() -> UIViewController? {
- return nil;
- }
- //设置当前ViewController的StatusBar的样式
- override func preferredStatusBarStyle() -> UIStatusBarStyle {
- return .Default
- }
- //隐藏还是展示statusBar
- override func prefersStatusBarHidden() -> Bool {
- return true
- }
- //statusBar的改变动画
- override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
- return .Fade
- }
- //当statusBar的状态改变后需要调用刷新
- // setNeedsStatusBarAppearanceUpdate()
Navigation相关
- override func setToolbarItems(toolbarItems: [AnyObject]?, animated: Bool) {
- }
- self.navigationItem
- self.editButtonItem()
- hidesBottomBarWhenPushed = true
- self.toolbarItems = nil
TabBar相关
- self.toolbarItems
常量
- UIModalTransitionStyle
- Modal Presentation Styles
- UIViewControllerHierarchyInconsistencyException
- UIViewControllerShowDetailTargetDidChangeNotification