如何将新视图控制器添加到与父项具有相同特征的容器中?

时间:2014-12-16 19:34:08

标签: ios swift

因此,希望有人可能熟悉本指南,了解如何创建滑出侧栏菜单

http://www.raywenderlich.com/78568/create-slide-out-navigation-panel-swift#comments

我已遵循本指南,并希望创建一个类似Venmo或groupme的应用程序。如果使用侧面导航面板作为菜单,并且当单元格被轻敲时,它会转移到新的视图控制器,顶部的导航栏与父控制器相同。

解决这个问题的正确方法是什么?

这是我的容器代码 centerview是我猜的“主页”的初始视图控制器 sidepanel是作为tableview的滑出菜单。

我想创建一个新的VC让它调用newViewController,当我点击侧面板中的一个单元格时,它会转换为newViewController,它具有与中心视图相同的导航栏和功能,但显然具有不同的内容。

import UIKit
import QuartzCore

enum SlideOutState {
    case BothCollapsed
    case LeftPanelExpanded
    case RightPanelExpanded
}





let centerPanelExpandedOffset: CGFloat = 80


class ContainerViewController: UIViewController, CenterViewControllerDelegate {

    var currentState: SlideOutState = .BothCollapsed
    var leftViewController: SidePanelViewController?
    var centerNavigationController: UINavigationController!
    var centerViewController: CenterViewController!
    var browseFlavorNavigationController: UINavigationController!
    var browseFlavorViewController: CenterViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        centerViewController = UIStoryboard.centerViewController()
        centerViewController.delegate = self

        // wrap the centerViewController in a navigation controller, so we can push views to it
        // and display bar button items in the navigation bar
        centerNavigationController = UINavigationController(rootViewController: centerViewController)
        view.addSubview(centerNavigationController.view)
        addChildViewController(centerNavigationController)

        centerNavigationController.didMoveToParentViewController(self)
    }

    // MARK: CenterViewController delegate methods

    func toggleLeftPanel() {

        let notAlreadyExpanded = (currentState != .LeftPanelExpanded)

        if notAlreadyExpanded {
            addLeftPanelViewController()
        }

        animateLeftPanel(shouldExpand: notAlreadyExpanded)
    }

    func collapseSidePanels() {
        switch (currentState) {
        case .LeftPanelExpanded:
            toggleLeftPanel()
        default:
            break
        }
    }

    func addLeftPanelViewController() {

        if (leftViewController == nil) {
            leftViewController = UIStoryboard.leftViewController()
            leftViewController!.navigation = Settings.allSettings()

            addChildSidePanelController(leftViewController!)
        }
    }
    func addChildSidePanelController(sidePanelController: SidePanelViewController) {
        view.insertSubview(sidePanelController.view, atIndex: 0)

        addChildViewController(sidePanelController)
        sidePanelController.didMoveToParentViewController(self)
    }

    func addRightPanelViewController() {
    }

    func animateLeftPanel(#shouldExpand: Bool) {
        if (shouldExpand) {
            currentState = .LeftPanelExpanded

            animateCenterPanelXPosition(targetPosition: CGRectGetWidth(centerNavigationController.view.frame) - centerPanelExpandedOffset)
        } else {
            animateCenterPanelXPosition(targetPosition: 0) { finished in
                self.currentState = .BothCollapsed

                self.leftViewController!.view.removeFromSuperview()
                self.leftViewController = nil;
            }
        }
    }

    func animateCenterPanelXPosition(#targetPosition: CGFloat, completion: ((Bool) -> Void)! = nil) {
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .CurveEaseInOut, animations: {
            self.centerNavigationController.view.frame.origin.x = targetPosition
            }, completion: completion)
    }


    func animateRightPanel(#shouldExpand: Bool) {
    }

    // MARK: Gesture recognizer

    func handlePanGesture(recognizer: UIPanGestureRecognizer) {
    }
}

private extension UIStoryboard {
  class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }

  class func leftViewController() -> SidePanelViewController? {
    return mainStoryboard().instantiateViewControllerWithIdentifier("LeftViewController") as? SidePanelViewController
  }

  class func rightViewController() -> SidePanelViewController? {
    return mainStoryboard().instantiateViewControllerWithIdentifier("RightViewController") as? SidePanelViewController
  }

  class func centerViewController() -> CenterViewController? {
    return mainStoryboard().instantiateViewControllerWithIdentifier("CenterViewController") as? CenterViewController
  }
}

0 个答案:

没有答案