简体   繁体   中英

Status bar color changed to white in custom navigation drawer in iOS?

I have created a custom navigation drawer in swift. I have used the window from app delegate & added a view on winddow. After i hide show the view on button click.

Below code to create drawer.

func setupSideMenu(){

        windowSideMenu = ((UIApplication.shared.delegate?.window)!)!
        windowSideMenu.backgroundColor = UIColor.black
        if customView == nil {
            print("custom view nil")
            // Register Swipe gesture for opening slideMenu
            let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.openSideMenu))
            swipeRight.direction = UISwipeGestureRecognizerDirection.right
            self.view.addGestureRecognizer(swipeRight)

            /// Register Drawable NIB here only for once
            customView = SideView.instanceFromNib() as! SideView
            customView.configureDrawer()
            customView.navigationController = self.navigationController
            customView.frame = CGRect(x: -customView.frame.size.width, y: -10, width: self.view.bounds.size.width - 30, height:UIScreen.main.bounds.height)
            customView.drawerDelegate = self

            /// BackView (DimView)
            backView.frame = self.view.frame
            backView.backgroundColor = UIColor.black
            backView.alpha = 0.4
            backView.isHidden = true
            self.view.addSubview(backView)
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.closeSideMenu))
            backView.addGestureRecognizer(tapGesture)
            customView.isHidden = true
            windowSideMenu.addSubview(customView)
            windowSideMenu.makeKeyAndVisible()

        }
    }

On hide & show i change the window level UIWindowLevel .

  override func openSideMenu() {
        super.openSideMenu()
        self.windowSideMenu.windowLevel = UIWindowLevelStatusBar
    }

    override func closeSideMenu() {
        super.closeSideMenu()
        self.windowSideMenu.windowLevel = UIWindowLevelNormal

    }

But when i change the windowlevel to UIWindowLevelStatusBar then the color of status bar is setting white.

Here is the screen shot for drawer 在此输入图像描述

Sorry i had to change some colors as i can't show the whole design.

As i came to understand the problem because of sidemenu to overcome this issue we have one library very easy to customize

To set or to change root, left or right view controllers or views, call:

sideMenuController.rootViewController = rootViewController
sideMenuController.leftViewController = leftViewController
sideMenuController.rightViewController = rightViewController

sideMenuController.rootView = rootView
sideMenuController.leftView = leftView
sideMenuController.rightView = rightView

Hope this will help you

Maybe because your SideView has a white background. Try changing that in the storyboard

So far as I can tell you are trying to keep the status bar the same color (black). But when the SlideMenu's window level is changed it turns to it's default white. I also see you are setting customView.navigationController = self.navigationController. the navigationController is a class so it is passed by reference not by a copy, but for some reason the navigation bar color is being reset to it's default white, (perhaps another VC's color is white and it is using that, set the color in other VC's to determine if this is the cause) try reseting the color after assigning the navigation controller:

 customView.navigationController?.navigationBar.tintColor = UIColor.black

Also, you should be making ui changes async as to avoid blocking the main thread. Since UIWindowLevelStatusBar is a CGFloat you set it like so to avoid collision with the actual status bar.

        DispatchQueue.main.async(execute: {
            if let window = UIApplication.shared.keyWindow {
                window.windowLevel = UIWindowLevelStatusBar + 1
            }
          }


        DispatchQueue.main.async(execute: {
            if let window = UIApplication.shared.keyWindow {
                window.windowLevel = UIWindowLevelNormal
            }
          }

A screen shot of the view hierarchy in the debugger would also be helpful in determining the root cause. Hope this helps, good luck!

Are you try implementing the prefersStatusBarHidden method on the root view controller of your UIWindow ? Maybe it can help to you problem.

For example:

- (UIStatusBarStyle) preferredStatusBarStyle {
    return UIStatusBarStyleDarkContent;
}

You can check how this library manage show/hide status bar when the drawer menu appears SlideMenuController.swift , I think is the same effect that you want: ( Gift ). I'am using it right now and it's very easy to customize.

When the menu is open:

...
DispatchQueue.main.async(execute: {
            if let window = UIApplication.shared.keyWindow {
                window.windowLevel = UIWindowLevelStatusBar + 1
            }
        })

When the menu is closed:

...
DispatchQueue.main.async(execute: {
            if let window = UIApplication.shared.keyWindow {
                window.windowLevel = UIWindowLevelNormal
            }
        })

Hope this will help you, regards!

For your answer different possibilities is there..

POSSIBLE WAY : 1
If you are using xcode 9.0+ so iPhone X is available in simulator and new concept of safe view is there, so you give top constraint to the safe view so this white strip available behind the status bar. (If mention detail related to you so solution is give constraints to the main view not to the safeView) or you can also try below code..

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

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.black

        return true
    }

POSSIBLE WAY : 2
And one library is available for this slide effect. It's name is SlideMenuControllerSwift

In this library different option is available so from them you can change status bar.

SlideMenuOptions.contentViewScale = 1.0
        SlideMenuOptions.leftViewWidth = UIScreen.main.bounds.size.width * 0.6

        let leftVC = UIStoryboard().getViewController(viewController: "LeftView") as! LeftView
        let mainVC = UIStoryboard().getViewController(viewController: "MenuView")

        let slideMenuController = SlideMenuController(mainViewController: mainVC, leftMenuViewController: leftVC)
        SlideMenuOptions.hideStatusBar = false

        self.window?.rootViewController = slideMenuController
        self.window?.makeKeyAndVisible()

POSSIBLE WAY : 3 在此输入图像描述 So add this into info.plist and then apply below changes as per your requirement. 在此输入图像描述

So from this 3 ways try any one may it help you to solve your problem. Happy coding : )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM