问题描述
几天来我一直在尝试向左滑动菜单.我找不到任何可与我的应用程序一起使用的库,因此我求助于raywenderlich的教程:
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.
我尝试了"self.view.bringSubviewToFront(navigationController.navigationBar)",但这没用.
I tried "self.view.bringSubviewToFront(navigationController.navigationBar)" But that didn't work.
以下是我要完成的操作的屏幕截图.
Here's a screenshot of what I'm trying to accomplish.
我当前的设置是通过appDelegate's
didFinishLaunchingWithOptions`中设置的ContainerViewController
进行的:
My current setup is made using a ContainerViewController
that's set in my appDelegate's
didFinishLaunchingWithOptions`:
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let containerViewController = ContainerViewController()
window!.rootViewController = containerViewController
window!.makeKeyAndVisible()
return true
此ContainerViewController将获得2个childViewControllers.
This ContainerViewController is getting 2 childViewControllers.
CenterViewControllerController(我的内容)
the CenterViewControllerController (my content)
CenterViewController将正确设置navigationController.
The CenterViewController will have the navigationController correctly set up.
var centerNavigationController: NavigationController!
var centerViewController: CenterViewController!
在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)
}
和SidePanelViewController(我的菜单及有问题的菜单.)
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)
}
SidePanelViewController
当前没有导航控制器.我确实尝试添加它.但这并没有达到预期的结果.
The SidePanelViewController
currently doesn't have a navigationController. I did try adding it. But that did not have the desired results.
基于注释,我需要做的是使ContainerViewController成为一个具有navigationController的容器,因此,这两个childController也应该具有该功能?但是我不知道如何在该级别上添加navigationController.
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)
我尝试过的图书馆:
ENSwiftSideMenu(我想要的是什么,但是花了2天的时间使它没有任何效果)
ENSwiftSideMenu (did what I want. But spent 2 days trying to make it work with no results)
Brian Advent YouTube教程
Brian Advent youtube tutorial
Dekatotoro的SlideMenuControllerSwift
Dekatotoro's SlideMenuControllerSwift
wanaya/幻灯片菜单
wanaya/Slide-Menu
Swift中的SWRevealViewController
SWRevealViewController in Swift
推荐答案
我将使用嵌入在导航控制器中的自定义容器控制器来执行此操作(以获取导航栏).该控制器将具有两个容器视图,左视图的宽度为屏幕宽度的80%,右视图的宽度为整个屏幕的宽度.这些可以在情节提要中设置.添加到容器控制器视图中的滑动手势识别器将使您可以滑动打开和关闭的菜单.下面的代码是容器视图控制器中打开和关闭菜单所需要的全部代码.出口leftCon是左侧容器视图的左侧边缘与其上级视图之间的约束,
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()
}
}
}
我有一个示例应用程序( http://jmp.sh/5QwhYov )演示了控制器,但未解决如何根据菜单控制器中的选择更改主控制器中的内容.有多种方法可以实现,如果您可以给我更多有关如何显示的详细信息,我可以解决.
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.
这篇关于快速滑出菜单而无需滑动导航栏(以编程方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!