cheriana cheriana - 3 months ago 169
iOS Question

How to set Xamarin iOS navigation and status bar color

I'm new to Xamarin iOS Designer and am trying to set the status bar and navigation controller background color as shown in this example: https://www.youtube.com/watch?v=DApI7aWGNiY

I've opened my main storyboard in Xcode Interface Builder and followed the steps in the tutorial, adding code to appDelegate.m. Code is as follows:

#import "AppDelegate.h"
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];

return YES;
}

@end


The above doesn't seem to work - the status and navigation bar remain gray when I save and build in Xamarin.

I'm wondering why and if it has anything to do with the fact that the project was created in Xamarin and not Xcode. My .m file resides in a "supporting files" folder unlike the demo and it doesn't contain any of the auto-generated code. There seems to be much better documentation on Xcode Interface Builder which is why I have been using that instead of the newer Xamarin iOS Designer.

Xamarin Studio 5.1.3 and Xcode 5.1.1
Any tips? Thanks!

**Update: Was able to achieve the background color change on both status and nav bar by adding
this.NavigationController.NavigationBar.BarTintColor = UIColor.Yellow;
to my viewcontroller.cs right after
base.ViewDidLoad ();
. Would still like to know why code added to the Xcode project .m files does not render in the Xamarin build.

Answer

If you want to change the navigation bar color throughout your entire iOS app add this to your AppDelegate FinishedLaunching method:

UINavigationBar.Appearance.BarTintColor = UIColor.YourColor;

You can also edit the text attributes for the navigation bar by doing the following:

UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White });

As for the status bar if you want to change the status bar icons to white instead of default black, for example, create a class for your navigation controller and override UIStatusBarStyle

public override UIStatusBarStyle PreferredStatusBarStyle ()
{
     return UIStatusBarStyle.LightContent;
}

This would work for your NavigationController and any child controllers of it.