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
-
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.