Skip to content

2 Coloring the Navigation Bar Text on iOS

In a previous blog post, we covered how to set the color of the NavigationBar on a MobileScreen in iOS projects. However, if you’re customizing the Navigation Bar’s background, you’ll likely want control over the title text color and the color of any buttons added to it as well.

Continue reading to learn how to customize those elements too.

Just like in the previous blog post, we’ll need to use several Declares to access the necessary iOS framework functions and retrieve the underlying objects we want to modify.

If you haven’t followed the previous post yet, be sure to check it out first—we’ll be building on the same example project to add these new customizations.

Let’s add a new method to our previously created “DeclaresForiOS” Module using the following values:

  • Method Name: NavigationBarTextColor
  • Parameters: Extends screen As MobileScreen, Assigns value As ColorGroup
  • Scope: Global

And add the following snippet of code in the associated Code Editor:

If value = Nil Then Return

Var controller As Ptr = screen.ViewControllerHandle

Declare Function NavigationController Lib "UIKit" Selector "navigationController" (controller As Ptr) As Ptr
Declare Function NavigationBar Lib "UIKit" Selector "navigationBar" (controller As Ptr) As Ptr

Var nc As Ptr = NavigationController(controller)
Var nv As Ptr = NavigationBar(nc)

Var colPtr As Ptr
colPtr = ColorGroupToUIColor(value)

Declare Function DictionaryWithObjectForKey Lib "UIKit" Selector "dictionaryWithObject:forKey:" (obj As Ptr, value As Ptr, key As CFStringRef) As Ptr

// https://developer.apple.com/documentation/foundation/nsclassfromstring(_:)?language=objc
Declare Function NSClassFromString Lib "Foundation" (name As CFStringRef) As Ptr

Declare Function StandardAppearance Lib "UIKit" Selector "standardAppearance" (obj As Ptr) As Ptr
Declare Sub SetStandardAppearance Lib "UIKit" Selector "setStandardAppearance:" (obj As Ptr, value As Ptr)
Declare Sub SetScrollEdgeAppearance Lib "UIKit" Selector "setScrollEdgeAppearance:" (obj As Ptr, value As Ptr)

Declare Sub SetTitleTextAttributedText Lib "UIKit" Selector "setTitleTextAttributes:" (obj As Ptr, value As Ptr)
Declare Sub SetLargeTitleTextAttributedText Lib "UIKit" Selector "setLargeTitleTextAttributes:" (obj As Ptr, value As Ptr)

Declare Sub SetTintColor Lib "UIKit" Selector "setTintColor:" (obj As Ptr, value As Ptr)

// We need to create a NSDictionary with the attribute we want to set on text for the NavigationBar Appearance
Var dict As Ptr = DictionaryWithObjectForKey(NSClassFromString("NSMutableDictionary"), colPtr, "NSColor")

Var appear As Ptr = StandardAppearance(nv)

// Setting the new appearance settings both for the regular-sized title
// and the Large one
SetTitleTextAttributedText(appear, dict)
SetLargeTitleTextAttributedText(appear, dict)

// And we apply the modified appearance to the StandardAppearance
SetStandardAppearance(nv, appear)

// And to the scrollEdgeAppearance if the app is run on iOS 15+
If (System.Version.MajorVersion >= 15.0) Then
   SetScrollEdgeAppearance(nv, appear)
End If

// The TintColor is applied on the text of the added buttons to the NavigationBar
// So we need to use the same color for them!
SetTintColor(nv, colPtr)

And that’s all the code we need.

Colorizing!

Select the Screen1 item in the Navigator and add a new property to it using the following values:

  • Name: MyNavigationBarTextColor
  • Type: ColorGroup
  • Scope: Protected

Now select the Screen1.Opening event and add the following line of code:

MyNavigationBarTextColor = New ColorGroup(Color.Yellow, Color.Red)

Finally, select the Screen1.AppearanceChanged event handler and add the following line:

Me.NavigationBarTextColor = MyNavigationBarTextColor

Run the example project, and you’ll see that the title text remains correctly displayed, even when switching between Light and Dark modes.

Download the example project from this link.

Javier Menendez is an engineer at Xojo and has been using Xojo since 1998. He lives in Castellón, Spain and hosts regular Xojo hangouts en español. Ask Javier questions on Twitter at @XojoES or on the Xojo Forum.