[英]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
? 您是否尝试在
UIWindow
的root 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.