-1

I have a screen in my iOS app that has side menu, when I swipe this side menu I want it to cover the status bar ( but I don't want status bar to be hidden completely ), I just what the part that overlaps with side menu, to get under side menu, not front of it, can anyone help me? (I'm using swift 4.2 in my app) (this side menu is just another ViewController that I animate in and out of my MainViewController)

  • no, it's just a ViewController that I add another ViewController as it's child and animate it inside and outside with a button action or swipe –  Dec 08 '18 at 15:02

1 Answers1

0

A possible way to show the side menu over the status bar is to use a UIWindow with wndowLevel = .statusBar that will present the status menu UIViewController. Here is a quick implementation I made:

func presentSideMenu() {
    let vc = UIViewController() // side menu controller
    vc.view.backgroundColor = .red
    window = UIWindow()
    window?.frame = CGRect(x: -view.bounds.width, y: 0, width: view.bounds.width, height: view.bounds.height)
    window?.rootViewController = vc
    window?.windowLevel = .statusBar
    window?.makeKeyAndVisible()
    window?.isHidden = false
    window?.addSubview(vc.view)
}

Then you can add a pan recognizer to your view and change the frame of the UIWindow accordingly. Again a simple snippet:

func hideSideMenu() {
    window?.isHidden = true
    window = nil
}

@objc func pan(recognizer: UIPanGestureRecognizer) {
    if recognizer.state == .began {
        presentSideMenu()
    } else if recognizer.state == .changed {
        let location = recognizer.location(in: view)
        window?.frame = CGRect(x: -view.frame.width + location.x, y: 0, width: view.frame.width, height: view.frame.height)
    } else if recognizer.state == .ended {
        hideSideMenu()
    }
}

Note that you should hold a strong reference to the UIWindow otherwise it will be released immediately. Maybe you should consider if presenting over the status bar is a good idea though. Hope this helps.

gulyashki
  • 449
  • 3
  • 5