MapView XCode中的侧边菜单

时间:2018-04-03 09:47:46

标签: ios xcode mkmapview swrevealviewcontroller side-menu

如何在打开时添加Uber等地图中正常运行的侧边菜单?我希望能够单击显示选项的“侧面菜单”。我遇到麻烦的主要问题是我可以“拖动”菜单来折叠它,就像Uber应用程序的主页一样。我使用“SWRevealViewController”来创建侧面菜单。

以下是我的尝试:

    override func viewDidLoad() {
        super.viewDidLoad()

        self.MapView.showsUserLocation = true

        self.revealViewController().tapGestureRecognizer()

menuButton.target = revealViewController()
            menuButton.action = 

#selector(SWRevealViewController.revealToggle(_:))
                view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())


       self.MapView.isZoomEnabled = false;
            self.MapView.isScrollEnabled = false;
            self.MapView.isUserInteractionEnabled = false
    }

当按下menuButton时会出现侧边菜单,但我似乎无法“拖动”它来折叠它。我认为地图的功能(如缩放,移动等)需要以某种方式被禁用所以它不会干扰侧面菜单的“拖动”

1 个答案:

答案 0 :(得分:0)

为此,您可以使用SlideMenuControllerSwift代替SWRevealViewController

无论如何你的情况都很简单,你的情况非常简单,因为当你尝试使用菜单时,你的情况非常简单,因此当你尝试使用菜单时它不起作用。

要实施SlideMenuControllerSwift,您需要执行以下操作,

在您的pod文件中添加pod,

pod 'SlideMenuControllerSwift'

成功安装后,添加两个View控制器YourMapVCLeftMenuVC,并在AppDelegate文件中添加以下方法,

func createMenu() {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let yourMapVC = storyboard.instantiateViewController(withIdentifier: "YourMapVC") as! YourMapVC
        let leftMenuVC = storyboard.instantiateViewController(withIdentifier: "LeftMenuVC") as! LeftMenuVC

        let nvc = UINavigationController(rootViewController: yourMapVC)

        let slideMenuController = SlideMenuController(mainViewController: nvc, leftMenuViewController: leftMenuVC, rightMenuViewController: UIViewController())
        slideMenuController.automaticallyAdjustsScrollViewInsets = true
        self.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
        self.window?.rootViewController = slideMenuController
        self.window?.makeKeyAndVisible()
    }

didFinishLaunchingWithOptions方法AppDelegate之后调用上述方法,如下所示

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        self.createMenu()
        return true
    }

添加菜单按钮在项目中任意位置的UINavigationBar位置。

extension UIViewController {

    func setupNagivationBar() {

        self.removeNavigationBarItem()
        self.setNavigationBarItem()

    }

    func setNavigationBarItem() {

        self.slideMenuController()?.removeLeftGestures()
        self.slideMenuController()?.removeRightGestures()


        self.addLeftBarButtonWithImage(UIImage(named: "icon_menu")!)
        self.slideMenuController()?.addLeftGestures()
        self.slideMenuController()?.removeRightGestures()
        self.slideMenuController()?.closeRight()

        self.navigationController?.navigationBar.barTintColor = UIColor.gray
    }



    func removeNavigationBarItem() {
        self.navigationItem.leftBarButtonItem = nil
        self.navigationItem.rightBarButtonItem = nil
        self.slideMenuController()?.removeLeftGestures()
        self.slideMenuController()?.removeRightGestures()
    }


}

并在setupNagivationBar() YourMapVC方法中调用viewDidLoad方法。

class YourMapVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupNagivationBar()

    }

}

此外,您需要将icon_menu图像放入项目中。

以下是您可以在屏幕截图中看到的结果。

YourMapVC

LeftMenuVC

以下是教程链接, https://github.com/dekatotoro/SlideMenuControllerSwift