Customising same UINavigationBar within app multiple times - ios

I am customising UINavigationBar with different color and custom font by using titleTextAttributes. However, when I moved to another view, I would like to use different color from previous with same custom font.
I have used,
[[UINavigationBar appearance] setTitleTextAttributes:mySettings]
in AppDelegate.m. When call same method with newSettings in viewDidLoad of another viewController, it doesn't get reflected.
I am able to change bar color or bar tint color in viewDidLoad of another viewController. However, my title foreground color is not changing. Am I missing anything?
Last solution which I have to have custom titleView. But wanted to avoid it. Any inputs?

[[UINavigationBar appearance] setTitleTextAttributes:mySettings] is a global configuration, so that's the reason why changing it in the other view controllers doesn't work. I believe you won't getting around customizing the titleView property of navigation item,... :/
You can do this by creating a UILabel that has the settings that you want and then assign it to the property:
NSAttributedString *title = [[NSAttributedString alloc] string:"the title" attributes:mySettings];
UILabel *newTitleView = [[UILabel alloc] init];
newTitleView.attributedText = title;
self.navigationItem.titleView = newTitleView;

Related

UIButton appearance setBackgroundColor effecting navigation bar now after update

Xcode or iOS update has made the following code below function differently because now the navigation bar button background has a background color, unlike previously. Any fix? I want all buttons to have the same global color, but now it effects the navigation bar buttons which I dont want. I would like it transparent like before.
[[UIButton appearance] setBackgroundColor:[Helper getColor:self.application.color]];
As this particular UIButton is inside a UINavigationBar you could try applying a second specific appearance to 'override' the first general appearance set.
For example calling specifically,
[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil]
setBackgroundColor:[UIColor clearColor] forState:state barMetrics:metrics];
after you call,
[[UIButton appearance] setBackgroundColor:[Helper getColor:self.application.color]];
To override the general with the specific.
Docs Link: https://developer.apple.com/documentation/uikit/uiappearance

Changing the colour of Navigation Controller bottom toolBar

I am trying to change the colour of the bottom bar of my Navigation Controller. I have managed to change the top NavBar in my appdelegate by adding
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0.688 green:0.437 blue:0.794 alpha:1.0]];
I want to apply this to the bottom bar also.
You can use [UIToolbar appearance] to set the appearance of UIToolbar
Good luck
I had the same problem, but could escape this by setting a tag for my navigationBar in my storyboard. Then I could change the navigationBar's color like this:
let navigationBar = (self.view.viewWithTag(someNumber) as UINavigationBar)
navigationBar.barTintColor = UIColor.blackColor()
This is not ideal unless you can guarantee that the number of subviews in your view is less than "someNumber". If you can though, then this should work.

How do you set the background color of a UINavigationbar programatically?

I am using a cocoa iOS component from cocoacontrols.com, which is NOT using storyboards. This class creates a new modal view and the modal view has a NavigationBar at the top. I am trying to get the color of the navigationBar to change. In all my other views, which are storyboard based, I use a base class of STBaseViewController which sets the navigationBar as white.
I have changed this cocoacontrol class to be a STBaseViewController instead of a UIViewController, but it still is not changing my background to white.
#interface BZPasscodeFieldController : STBaseViewController
In the BZPasscodeFieldController, which they don't make any reference to a navigationbar, so I am not sure how its even showing up (again it has no storyboard's only xibs and they don't have navigationbars)?
Anyway, does anyone know how to make the navigationbar background color white programatically?
Further, in all my storyboard viewControllers that have a UINavigationBar, I have added a UIButton over the area where the title goes, so that I can easily change the font/style and also make it clickable. I need to add this same UIButton to the uinavigationBar of this BZPasscodeFieldController created programatically. How would I go about doing that?
To Set Navigationbar Background Color:
[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
To set Navigationbar Tint Color:
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
For your question about the button on the place of title of navigationBar. Each navigationBar has a titleView property, you can assign any view to it. For example take a look at this method, you can call it in your BZPasscodeFieldController:
- (void) setNavigationBarTitleButton
{
UIButton* centralButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
[centralButton setImage:navigationCentralButtonImage forState:UIControlStateNormal];
[centralButton setShowsTouchWhenHighlighted:TRUE];
[centralButton addTarget:self action:#selector(goHigher) forControlEvents:UIControlEventTouchDown];
self.navigationItem.titleView = centralButton;
}
For your previous question the answers provided already are all correct, calling them in the right place(for example in AppDelegate's application:didFinishLaunchingWithOptions: should make it work well, unless something is messed up in your custom view controller classes.
[[UINavigationBar appearance] setTintColor:[UIColor blueColor]];
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];

Customizing UIBarButtonItems in various parts of applications (background image and shape)

I want to change the background image of my UIBarButtonItems. In the root view, I want them to be a certain background image (but still rounded-rect buttons) in the nav bar, but in the next view, I have a UIToolBar where I want it to have different backgrounds yet again.
I used [[UIBarButtonItem] appearance] in my app delegate to change all of them, but I now realize for some I want it one style and for others yet another.
More importantly, I want to change not only the background image, but the shape for some. For one of the UIToolBar's UIBarButtonItems I want it to be in the shape of a back button in the navigation bar. How would I achieve this look?
Can I achieve both of these with the method outlined here?
Basically: How do I make custom UIBarButtonItems all over the app, and have some with different shapes?
If you want the buttons to look different in different view controllers, you should set them within the view controller, not in the delegate. Add code in either the init method or the viewWillAppear: method to customise the buttons. If you are customising UIToolbar items then use code similar to the link you gave. If it's for the navigation bar, do something like this:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//Customise button with background etc that you want
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = item;
//Release item if you're not using ARC
You can use the 'appearanceWhenContainedIn' method from the UIAppearance Protocol http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html
So for the Toolbar, you'd use:
[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil]
And for the NavBar:
[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]

Change Nav Bar Title Font - DIFFERENT

As you can see in the picture below, my UIViewController IS NOT a UINavigationController, it's a common UIViewController. What I did is I put a UINavigationBar using interface builder and above it I put a UIImage. The problem is that I want to change the font of this UINavigationBar. Anyone would have a clue on how to do it?
Usually, with a common UINavigationController I use the following code:
// this will appear as the title in the navigation bar
self.label = [[UILabel alloc] initWithFrame:CGRectZero];
self.label.backgroundColor = [UIColor clearColor];
self.label.font = [UIFont fontWithName:#"Copperplate" size:22];
self.label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
self.label.textAlignment = UITextAlignmentCenter;
self.label.textColor = [UIColor whiteColor]; // change this color
self.label.text = [self.navigationItem title];
self.navigationItem.titleView = label;
[label sizeToFit];
Well it should work the same way. I think you just need an IBOutlet for the UINavigationBar, or only for the UINavigationItem (the title for your UINavigationBar) and that's it.
Storyboard Solution
There's nothing wrong with the answer above but a really simple way to do this is to select the Navigation Bar in the storyboard. Then change the Title Font in the attributes inspector.
Nota Bene
This technique is also really useful when you want to change the font
across an entire set of views whenever you are using a navigation
controller. (Just change it in one place). Xcode 7.1.1 has a couple of bugs. One of those requires that you toggle the Bar Tint from the default to another color (you can always reset it to the default if needed) in order to see the font change.
Custom Fonts
The above is currently not working when selecting a custom font (as of Xcode 7.1.1).
Please see the following SO Answer for a workaround if you need a
custom font. (tldr; add an outlet to a button or label, change the
custom font on that control, set that control as the
UINavigationItem.titleView).

Resources