Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
236 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...