How to color status bar in iOS?

How can I set the status bar color?
Is there a “normal” way? Or is there any declare?

If you mean the status bar at the top of the screen, you need to place a colored rectangle at the top of the view the is under it,
The statusbar itself cannot be subclassed… it has dark text or white text, and a clear background (unlike previous iOS where it was black or white).

Thank you very much, just what I needed!

There was a declare circulating during beta for 32 bit. I do not know if Jason King or Ulrich Bogun may have updated it for 64.

Except for the back button that has to be emulated, the rectangle trick works just fine, though.

Here’s how to set the navigation bar color:

sub setNavBarColor(v as iOSView, barColor as color, buttonColor as color, translucent as boolean = false)

  declare function NSClassFromString      lib "Foundation" (classname as CFStringRef) as ptr
  declare function keyWindow              lib "UIKit" selector "keyWindow"  (obj_ref as ptr) as ptr
  declare function sharedApplication      lib "UIKit" selector "sharedApplication"  (obj_ref as ptr) as ptr
  declare function rootViewController      lib "UIKit" selector "rootViewController"  (obj_ref as ptr) as ptr
  declare function navigationBar          lib "UIKit" selector "navigationBar"  (obj_ref as ptr) as ptr
  declare function navigationController lib "UIKit" selector "navigationController" (viewController as ptr) as ptr
  dim navigationControllerRef as ptr = navigationController(v.ViewControllerHandle)
  dim sApp as ptr = sharedApplication(NSClassFromString("UIApplication"))
  dim navBar as ptr = navigationBar(navigationControllerRef)
  declare sub setTintColor lib UIKitLib selector "setTintColor:" (id as ptr, UIColor as Ptr)
  setTintColor navBar, new UIColor(buttonColor)
  declare sub setBarTintColor lib UIKitLib selector "setBarTintColor:" (id as ptr, UIColor as Ptr)
  setBarTintColor navBar, new UIColor(barColor)
  if translucent then
    declare sub setTranslucent lib UIKitLib selector "setTranslucent:" (id as ptr)
    setTranslucent navBar
end sub

Call it like this:

SetNavBarColor myViewName, myBarColor, myButtonColor

It works fine in 64-bit too.

I’m having total failure trying to get the navbar black with white text in my app. It doesn’t seem anything works to set the navbar black.

In addition to the tintColor and barTintColor from above I’ve tried the barStyle as well, and nothing seems to change at all.

I can set the view background color and I can use an picture to draw my text in the font I want and get a cell background color, so I think I can kludge my way around getting the table itself styled, but I can’t for the life of me figure out how to do anything with the navBar.

I’m about ready to resort to killing the navBar and just putting a button on the screen somewhere.

If you’re using the setNavBarColor method that I posted above, make sure you are calling it in each view’s Activate event. It does not work in the Open event because that occurs before all the initialisation is complete.

Thanks Jason. I’m not sure what was happening before or why, but I tried it in the activate event and it worked. After that I was refactoring some other code and moved it into a method. I ended up calling the method from the open event and that seems to work as well. I probably had something missing before. I wasn’t entirely systematic about what I was trying. I have since removed quite a few lines of code that didn’t seem necessary and everything is still working.

This code was necessary to get white font and widgets in the navBar.

declare sub setBarStyle lib UIKitLib selector "setBarStyle:" (id as ptr, theStyle as integer) setBarStyle navBar,1

I figured out how to get the text in the cells white without using the draw stuff into a picture kludge. I made use of some handy functions found in iOSLib.

[code]dim theCell as AppleTableViewCell = AppleTableViewCell.MakefromPtr(theUITable.getCell(0,0))


You will most likely want to set the cells background and tint colors as well as the selected background color.

Thank you Kevin. That’s a good tip on setBarStyle - I’m going to add that to my project! :slight_smile:

I’m resurrecting this thread because I’ve noticed a change in behaviour between Xojo 2019 r1 and 2019 r3, which I’m guessing is due to Xojo linking to a newer version of the iOS SDK.

If you take a look at the screenshot and GIF, you’ll see that pushing from a view that doesn’t have a navigation bar to one that does shows the content of the first view underneath the navigation bar during the animation. This is new, we didn’t used to see this layering effect before.

The declares that I’m using to set these colours are from Jeremie’s very helpful iOSDesignExtensions, where SetNavBarColorXC is similar to Jason’s declare above and also includes the setBarStyle declare that Kevin mentioned. Here’s what I’m doing in all of my views’ Activate event:

App.SetWindowColorXC( graphicsFunctions.kColourBackground )
Self.SetBackgroundColorXC( graphicsFunctions.kColourWhite )
Self.SetNavBarColorXC( graphicsFunctions.kColourBackground, graphicsFunctions.kColourNavigationBarTint, False, True )

Commenting out Self.SetNavBarColorXC fixes the transparency problem, but leaves me with the default white background and black text.

Does anyone know what might have changed between Xojo versions? Is there anything that I can add to SetNavBarColorXC that will bring the old behaviour back?

You are correct that the SDK changed last year. It was necessary to expose all of the new dark mode stuff.

You are correct that the SDK changed last year. It was necessary to expose all of the new dark mode stuff.

Thanks, @Greg_O_Lone ! Do you know how to reinstate the old “I want the nav bar to be a solid colour rather than transparent” behaviour?

I have a confession to make: when I started this thread, I had missed the fact that Jeremie had already updated ViewExtensionsXC.SetNavBarColorXC with iOS 13-specific changes that fix this problem.

So, the solution is:

  1. Use the latest version of ViewExtensionsXC.SetNavBarColorXC.
  2. Feel embarrassed.
  3. Publicly admit embarrassment to try and help others avoid getting themselves into that situation in the first place.
1 Like

Where do I get SetWindowColorXC from? I have iosextensions but didn’t see that method

Where do I get SetWindowColorXC from? I have iosextensions but didn’t see that method

It’s in the ViewExtensionsXC module, ViewExtensionsXC.SetWindowColorXC. It actually extends App as iOSApplication rather than a view, though.