Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

ios - how side menu can cover part of status bar

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...