swift slide out menu without sliding the navigatio

2019-05-22 12:13发布

I've been trying to make a left slide-out menu for a couple of days. I couldn't get any of the libraries to work with my application, so I resorted to raywenderlich's tutorial:

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

However, it doesn't do exactly what I want. My main problem is that when I slide out the menu the navigation bar slides out with it. I need my navigation bar to stay where it is and only move the content underneath it.

I tried "self.view.bringSubviewToFront(navigationController.navigationBar)" But that didn't work.

Here's a screenshot of what I'm trying to accomplish.

enter image description here

My current setup is made using a ContainerViewController that's set in my appDelegate'sdidFinishLaunchingWithOptions`:

window = UIWindow(frame: UIScreen.mainScreen().bounds)

    let containerViewController = ContainerViewController()

    window!.rootViewController = containerViewController
    window!.makeKeyAndVisible()

    return true

This ContainerViewController is getting 2 childViewControllers.

the CenterViewControllerController (my content)

The CenterViewController will have the navigationController correctly set up.

var centerNavigationController: NavigationController!
var centerViewController: CenterViewController!

In the ContainerViewController viewDidLoad:

  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 = NavigationController(rootViewController: LandingPageVC())
    view.addSubview(centerNavigationController.view)
    addChildViewController(centerNavigationController)

    centerNavigationController.didMoveToParentViewController(self)

    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
    centerNavigationController.view.addGestureRecognizer(panGestureRecognizer)
  }

and the SidePanelViewController (my menu & the problematic one.)

var leftViewController: SidePanelViewController?

  func addLeftPanelViewController() {
    if (leftViewController == nil) {
        leftViewController = SidePanelViewController()

        addChildSidePanelController(leftViewController!)
    }
  }

  func addChildSidePanelController(sidePanelController: SidePanelViewController) {
    println("addChildSidePanelController")

    view.insertSubview(sidePanelController.view, atIndex: 0)

    addChildViewController(sidePanelController)
    sidePanelController.didMoveToParentViewController(self)
  }

The SidePanelViewController currently doesn't have a navigationController. I did try adding it. But that did not have the desired results.

Based on the comments, what I need to do is somehow making the ContainerViewController the one that has a navigationController, so both of it's childControllers should have that as well? However I have no clue how I would go about adding the navigationController on that level.

Any help would be greatly appreciated! (I'm not using storyboards)

Libraries I tried:

ENSwiftSideMenu (did what I want. But spent 2 days trying to make it work with no results)

Brian Advent youtube tutorial

Dekatotoro's SlideMenuControllerSwift

wanaya/Slide-Menu

SWRevealViewController in Swift

标签: ios menu swift
1条回答
霸刀☆藐视天下
2楼-- · 2019-05-22 12:32

I would do this with a custom container controller embedded in a navigation controller (to get the navigation bar). This controller would have two container views, a left one with a width of 80% of the screen width, and a right one that is full screen width; these can be set up in the storyboard. Swipe gesture recognizers added to the container controller's view would allow you to swipe the menu open and closed. The code below is all you would need in the container view controller to open and close the menu. The outlet, leftCon, is the constraint between the left edge of the left container view and its superview,

class ViewController: UIViewController {

    @IBOutlet weak var leftCon: NSLayoutConstraint!
    @IBOutlet weak var leftContainer: UIView!


    @IBAction func closeMenu(sender: UISwipeGestureRecognizer) {
        leftCon.constant = -leftContainer.frame.size.width
        UIView.animateWithDuration(0.3){
            self.view.layoutIfNeeded()
        }
    }

    @IBAction func openMenu(sender: UISwipeGestureRecognizer) {
        leftCon.constant = 0
        UIView.animateWithDuration(0.3){
            self.view.layoutIfNeeded()
        }
    }

}

I have an sample app (http://jmp.sh/5QwhYov) that demonstrates the setup of the controllers, but doesn't address how you would change the content in the main controller based on a selection in the menu controller. There are any number of ways that could be implemented, and I can address that if you can give me more details on how you want that to appear.

查看更多
登录 后发表回答