Change Nav Bar Title Font - DIFFERENT - ios

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).

Related

Can't remove UILabel from UINavigationController.view

I'm adding a tiny marker under my UINavigationController title so the user will know that the title is tappable. You can see in the code below how I add this label to the navigation bar.
_labelCalendarMenuArrow = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 5, 30, 10, 26)];
_labelCalendarMenuArrow.text = #" ̬";
_labelCalendarMenuArrow.font = [UIFont fontWithName:#"HelveticaNeue" size:30];
_labelCalendarMenuArrow.textAlignment = NSTextAlignmentCenter;
_labelCalendarMenuArrow.textColor = [UIColor whiteColor];
[self.navigationController.view addSubview:_labelCalendarMenuArrow];
The problem is that I'm unable to remove this UILabel from the navigationController.view when leaving this screen. In the code below you can see how I try a few methods for hiding or removing this UILabel, but none of them work... The UILabel will stay in the NavigationController until I go to a different stack of views and come back. Any advice?
- (void)viewWillDisappear:(BOOL)animated {
[_labelCalendarMenuArrow removeFromSuperview];
_labelCalendarMenuArrow = nil;
_labelCalendarMenuArrow.alpha = 0;
}
A simple solution can be to use HIDDEN property
- (void)viewWillDisappear:(BOOL)animated
{
_labelCalendarMenuArrow.hidden=YES;
}
What you are trying to do here is fairly horrific, adding a view within a parent navigation controller's view is against all sense.
Please read apple's human interface guidlines as there is a better solution to signifying that the title is clickable in there. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/
If you still insist on adding a label underneath the navigation bar's title label you should implement a titleView for the navigation item. Within that view you will have to include your own title label to replace the original and then your signifier label underneath.

Customising same UINavigationBar within app multiple times

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;

Remove back button text and center title in navigation bar

I've removed the text for UIBarButton in AppDelegate:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -1000.f) forBarMetrics:UIBarMetricsDefault];
Which works like a charm:
As you can see, this doesn't align the navigation title at horizontal center. What is the best solution to accomplish this globally for all views.
PS: I am using Storyboard.
You can create your own custom titleView with a UILabel as follows:
UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0,0,100,50)];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,50)];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = #"PAKKELISTE";
[titleView addSubview:titleLabel];
self.navigationItem.titleView = titleView;
The details of the frames, text, alignment, etc. are just an example. The main idea though is that you set a custom UIView as the navigationItem's titleView.
It could also be an issue with your back button offset. Try this approach instead for removing the "back" text (I haven't tried this before, but I'm curious if it will work).
self.navigationController.navigationBar.topItem.title = #"";

change navigationbar button colors for ios7

I am trying to find out how to change the text and arrow color of the UINavigationBar buttons.
I know how to change the titleTextAttributes but cannot find out how to do it to the buttons.
For example this is how I change the titleTextAttributes inside my AppDelegate
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor redColor]};
I have looked through the UINavigationBar docs and cannot find any button attributes so not sure what to do here.
to change the text color and the color of the arrow in the navigation bar you can use this code (iOS 7):
self.navigationController.navigationBar.tintColor = [UIColor redColor];
this code will also change the color of the buttons
You can globally change it as easy as this

iOS accessibility - How do you set the accessibility label for the title of a UINavigationBar?

Apple's voice over mispronounces the title of one of my views, which is inside a UINavigation Controller.
In other parts of the app I have added a custom accessibility label to help it pronounce the company name correctly. How can I set the accessibility label of a UINavigationBar?
This works in iOS 8.2. In viewDidLoad:
self.navigationItem.accessibilityLabel = #"My accessible label";
When a navigation controller transitions to the view controller, the accessibilityLabel is read instead of the view controller title.
I couldn't add an accessibility label, but I found a workaround:
I replace the navigationItem's title View with a UILabel that has accessibility set up.
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"myTitle";
[titleLabel setAccessibilityLabel:#"myCustomAccessiblityLabel"];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;
I'm not sure why setting the accessibility label doesn't work, but the above code works for my needs.
Since UINavigationBar inherits from UIView, you should be able to set its accessibilityLabel property. Try: yourUINavigationBar.accessibilityLabel = #"title";.
Also, you may need to ensure sure it is marked as an accessibility element with yourUINavigationBar.isAccessibilityElement = YES; (and is not inside another view which is also marked as an accessibility element). (I'm guessing this last bit may be the issue, since it appears that you already knew about accessibility labels. You can use the Accessibility Inspector in the simulator to see if this is the case by looking at the box around the element when you tap it to see if it's around something bigger than the navigator bar.)
There a similar workaround which I have tested on iOS 11 and 12 that works as expected. For the navigationItem.titleView set a UILabel object and then set your accessibility identifier for the navigationItem.titleView.
self.navigationItem.titleView = YOUR_CUSTOM_UILABEL;
accessibilityIdentifier part
self.navigationItem.titleView.accessibilityIdentifier = YOUR_ACCESSIBILITY_IDENTIFIER;
You can see your identifier with using Xcode's Accessibility Inspector.

Resources