Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
UI: How to change status bar color when hide_title_bar=True
-
Hi there,
I am looking to be able to change the color of the status bar to black, to make it visible over the white background of my UI.view. When searching for ways to do this, I found you can simply use title_bar_color when presenting the view in fullscreen mode (which works). The issue is that for my program, I would like to have the title bar hidden, but when I do this the status bar goes back to white.
I have noticed this only happens when my phone is in dark mode, and when the device is in light mode the status bar is black by default and works correctly. I tried changing the overrideUserInterfaceStyle property in the ObjCInstance of my UIView to force light mode behaviour, but this doesn't seem to work, even though I have used overrideUserInterfaceStyle to successfully change the color of other UI elements. I think it may work if I did something like https://stackoverflow.com/questions/38740648/how-to-set-status-bar-style-in-swift-3#answer-40066798 to set the preferred status bar color in the ViewController, however I was unable to find how to override this method using objc_util.
Let me know if there are any more details or code that you would like me to provide, and thank you so much for the help!
-
This post is deleted! -
You can access the status at -- or used to -- using
UIApplication.sharedApplication().statusBar()
Then you should be able to set the backgroundColor property using UIColor.blackColor(), etc.
(Do this in the console so you can access tab completion)
-
Thanks so much for the reply! I tried accessing the status bar using this method however Pythonista immediately crashed. When I tried building my program on Xcode as an app, it gave me the following error:
Terminating app due to uncaught exception ‘NSInternalInconsistencyException‘, reason: ‘App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there’s no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.’
I'm wondering if something from https://stackoverflow.com/questions/56651245/how-to-change-the-status-bar-background-color-and-text-color-on-ios-13 might work but I'm not quite sure where to begin.
Update: I figured out that you can set the status bar color in Xcode in the info.plist file. Since I eventually just want to build this program into an app, that will solve the problem for me!
-
Ok, this might work (untried ... Might need to poke around a bit):
app=UIApplication.sharedApplication() kw=app.keyWindow() sbm=kw.windowScene().statusBarManager() F=sbm.statusBarFrame() statusBar=UIView.initWithFrame_(F) statusBar.backgroundColor = redColor() kw.addSubview_(statusBar)
There might be a new way to get the key window (via connectedScenes.windows).
-
Thank you so much for your help, it works great! As a follow-up, do you happen to know how to change the color of the text and icons themselves (clock, battery icon, etc.)? I would like to have white text when the background is black, and vice versa.
For anyone else interested, I tested a bit and made a few changes, the final code is:
# Find the window (I think I remember seeing somewhere that the keyWindow method is # deprecated, so I used this code for finding windows that I saw somewhere else) for window in UIApplication.sharedApplication().windows(): # Note, there are multiple windows, one is the window of the actual Pythonista application. I had # if 'PA3PythonistaWindow' in str(window._get_objc_classname()): # but this seems to only change the pythonista application and not my app's window. # For now, I am just running the following code on all windows in the loop and it works: F = window.windowScene().statusBarManager().statusBarFrame() statusBar = ObjCClass('UIView').alloc().initWithFrame(F) statusBar.backgroundColor = UIColor.redColor() window.addSubview_(statusBar)
-
I think windows have a isKeyWindow() -- you only need to do this on the key. Also, maybe want to check for existence of a subview with the same frame or assign it a tag, etc, to avoid creating duplicates..
There may bea way to set light vs dark in UINavigationBar
-
Ok,depending on the info plist setting:
The sharedApplication().setStatusBarStyle_(1,True)
Or
sharedApplication().setStatusBarStyle_(3,True)Might do what you want. But if it is set to view controller based, your VC must implement
preferredStatusBarStyle()
To return 1 for light, 3 for dark, and call setNeedsStatusBarAppearanceUpdate()
Thua it might be necessary to swizzle that method.
-
Thanks so much for your help! setStatusBarStyle_ with the True argument crashed for some reason, it said the function only expected one argument. When I removed the True and just put in the number that seems to work fine!
-
-
@cvp yes setStatusBarStyle is probably also deprecated. Don't use it if you plan to publish to app store, you might get rejected.
-
@JonB thanks, that’s good to know. I think there would be a way to set that in info.plist too, so maybe I’ll just go with that for now since it shouldn’t need to change colours while running. Thanks again for all your help!
Update: Yes, there is a way to change this in the info.plist file so I removed the setStatusBarStyle call for now.