简体   繁体   English

iOS中的自定义导航抽屉中状态栏颜色是否变为白色?

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

I have created a custom navigation drawer in swift. 我在swift中创建了一个自定义导航抽屉。 I have used the window from app delegate & added a view on winddow. 我使用了app delegate中的窗口并添加了关于挡风玻璃的视图。 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 . 在隐藏和显示时,我更改窗口级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. 但是当我将UIWindowLevelStatusBar更改为UIWindowLevelStatusBar ,状态栏的颜色设置为白色。

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. 也许是因为你的SideView有白色背景。 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. 但是当SlideMenu的窗口级别改变时,它将变为默认的白色。 I also see you are setting customView.navigationController = self.navigationController. 我也看到你正在设置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: navigationController是一个类,所以它通过引用传递而不是通过副本传递,但由于某种原因,导航栏颜色被重置为默认的白色(可能另一个VC的颜色是白色并且正在使用它,在其他颜色中设置颜色)用于确定这是否是原因的VC)在分配导航控制器后尝试重置颜色:

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

Also, you should be making ui changes async as to avoid blocking the main thread. 此外,您应该将ui更改为异步,以避免阻塞主线程。 Since UIWindowLevelStatusBar is a CGFloat you set it like so to avoid collision with the actual status bar. 由于UIWindowLevelStatusBar是CGFloat,因此您可以像这样设置它以避免与实际状态栏发生冲突。

        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 ? 您是否尝试在UIWindowroot view controller上实现prefersStatusBarHidden方法? 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 ). 当抽屉菜单出现时,您可以检查此库如何管理显示/隐藏状态栏SlideMenuController.swift ,我认为与您想要的效果相同:( 礼品 )。 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 可能的方式: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. 如果您使用的是xcode 9.0+,那么iPhone X在模拟器中可用,并且存在安全视图的新概念,因此您对安全视图给出了顶级约束,因此状态栏后面有此白色条带。 (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.. (如果提及与您相关的详细信息,那么解决方案是给主视图的约束而不是safeView)或者您也可以尝试下面的代码..

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 可能的方式:2
And one library is available for this slide effect. 此幻灯片效果可以使用一个库。 It's name is SlideMenuControllerSwift 它的名字是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 可能的方式:3 在此输入图像描述 So add this into info.plist and then apply below changes as per your requirement. 因此,将其添加到info.plist中,然后根据您的要求应用以下更改。 在此输入图像描述

So from this 3 ways try any one may it help you to solve your problem. 因此,从这3种方式尝试任何一种可能它可以帮助您解决您的问题。 Happy coding : ) 快乐编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS 7中使用Tint Color可以看到导航栏时,白色状态栏消失 - White status bar disappears when Navigation bar is visible with Tint Color in iOS 7 自定义iOS状态栏颜色 - Custom iOS Status Bar Color 将导航栏文本颜色和状态栏图标更改为白色 - Change navigation bar text color and status bar icons to white iOS自定义状态栏背景颜色不显示 - iOS Custom Status Bar Background Color not displaying iOS状态栏更改颜色以单独匹配导航栏 - iOS Status Bar changing color to match navigation bar on its own iOS7导航栏+状态栏文本颜色 - iOS7 Navigation Bar + Status Bar Text Color 如何在iOS 7上将状态栏的内容颜色设置为白色 - How to set status bar's content color to white on iOS 7 将Ionic 3应用程序的iOS状态栏字体颜色更改为白色 - Change iOS status bar font color to white for Ionic 3 app iOS 8.3:为什么MFMailComposeViewController的导航栏背景颜色仍然是白色? - iOS 8.3 : Why is the navigation bar background color for MFMailComposeViewController still white? iOS 11-当DisplayMode.primaryHidden更改为.allVisible时,SplitViewController主导航表视图更改状态栏颜色 - iOS 11 - SplitViewController Master Navigation Table View Changes Status Bar Color When DisplayMode.primaryHidden changed to .allVisible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM