Setting Navigation Bar Text Color in Xamarin iOS

February 1st has come and gone, and for Xamarin developers, a deadline has expired. All new App Store apps must be 64 bit compliant. So, all new submissions must use the Unified API. Xamarin has several tutorials and walk throughs, even a fantastic webinar about migrating to the new API. Each of their resources proved extremely valuable in migrating my current project to the new standard. While the Xamarin scripts to migrate to the new API handle most scenarios, I encountered a few that it didn't. This post covers the first one I noticed.

After running the migration scripts, I got this error in my Base View Controller, which has things like navigation bar color, default background color, etc.

image of error in xamarin studio

Type UIKit.UINavigationBar does not contain a definition for SetTitleTextAttributes and no extension method SetTitleTextAttributes of type UIKit.UINavigationBar could be found. Are you missing an assembly reference?

In the old days of the Classic API, we had access to a simple method called SetTitleTextAttributes, which took in an array of Text Attributes that controlled title bar appearance. When I commented out the offending line of code I was left with a title bar that looks like this.

image of bad title bar

The title of the view is not supposed to be black

In the Xamarin documents about the Unified API I found this little bit of knowledge.

The Unified API removes deprecated methods and there are a few instances where there were typos in the API names when they were bound to the original MonoTouch and MonoMac namespaces in the Classic APIs.

Apparently this method of modifying the default title bar does not work anymore. No problem right? My experience in Objective-C and iOS development tells me there is another way.

Instead of calling SetTitleTextAttributes, we now do the following:

this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes()
        {
            ForegroundColor = UIColor.White
        };

Which gives me the following, exceptional result.

image of pretty ui

In this post we covered how to change the color of the navigation bar title in the Xamarin Unified API.

comments powered by Disqus