Want to change NavigationItem title - ios

I've embedded my first view into a NavigationController and then i set the segue as push.
I want to change in each view the title of the Navigation bar to display something and update it through my code.
i tried to call the navigation controller like this:
self.navigationController.navigationBar.topItem.title = #"YourTitle";
but this wont work, how can I do it?

Navigation controller gets the title from the viewcontroller so
this will do the job
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Some Title";
}

This works for me:
self.navigationItem.title = #"YourTitle";
Hope it helps.

self.title = #"My Title"
This will change the navigation bars title as well as the title elements that refer to this view controller (eg. tab bar etc.)
self.navigationItem.title = #"My Title";
This will change only the title for the navigation bar for current view controller.

You can replace title view too:
UIView* titleView = [[UIView alloc] init];
[self.navigationItem setTitleView: titleView];

if u only change the navigation bar title then try this
on initwithnibname method
self.title=#"your title";
but you want to give a color or font size of title then u want to take a UIlable on it
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor blackColor];
self.navigationItem.titleView = label;
label.text =#"your title";
[label sizeToFit];

Related

How do you change the title color for a view controller pushed onto the stack?

From a UITableView I am pushing another view controller with another UITableView onto the stack using this code:
// allocate and create instance of categories view controller
TJKCategoriesViewController *categoriesViewController = [[TJKCategoriesViewController alloc] init];
// push it onto the top of the navigation controller's stack
[self.navigationController pushViewController:categoriesViewController animated:YES];
When I am in the viewDidLoad method for the TJKCategoriesViewContoller I am changing the title using this code:
self.title = #"Categories";
This works just fine. However the "Categories" title is coming out the color of black and I would like it to be a different color. I've tried things like tintColor, but self.title doesn't have this property.
Does anyone have any suggestions?
Thanks,
Glenn
You can create a UILabel and set it to UINavigationItem's titleView. See Apple doc: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationItem_Class/#//apple_ref/occ/instp/UINavigationItem/titleView
Some codes:
- (void)setMyTitle:(NSString *)title
{
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.navigationController.view.bounds.size.width - 100, 44)];
titleLabel.text = title;
titleLabel.font = [UIFont systemFontOfSize:16];
titleLabel.textColor = ...
...
self.navigationItem.titleView = titleLabel;
}
Add this to your viewDidLoad method:
NSArray *keys = [NSArray arrayWithObjects: NSForegroundColorAttributeName, NSFontAttributeName, nil];
NSArray *objs = [NSArray arrayWithObjects: [UIColor redColor], [UIFont fontWithName:#"HelveticaNeue-Bold" size:20.0f], nil];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
You can customize the font and the color
You can use the tintColor property of the NavigationBar:
self.navigationController.navigationBar.tintColor = [UIColor blueColor];
If you want to make tintColor changes to be global, you can use the NavigationBar appearance proxy.
[[UINavigationBar appearance] setTintColor: [UIColor blueColor]];

UIViewController title is always (null)

I have this problem. In my initial UIViewController, the title is always null. I have tried [self setTitile:] and many other ways of setting title, but nothing works. I suppose there should be the app name as title, and it should be set somewhere in Xcode maybe?
Here is how I set the title:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationItem setTitle:#"Your Title"];
//[self setTitle:#"Some title"];
. . .
}
I also set title in Storyboard, for the controller and the Nav controller that embeds it, but no luck.
Thank you.
Try below code..
UILabel *lbl = [[UILabel alloc] initWithFrame:self.navigationController.navigationBar.frame];
lbl.text = #"AppName";
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont systemFontOfSize:18];//18
lbl.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = lbl;
//(OR) try
self.navigationController.navigationBar.topItem.title = #"AppName";
Hope it helps you..!

Adjust font size in Navigation Title

Is there any way to make the font of the title of the navigation bar adjusted with the width?
Same behaviour as a [UILabel setAdjustsFontSizeToFitWidth:YES].
I'm trying setting the appearance of the NavigationBar, but no success so far.
Try doing something like:
self.navigationItem.titleView = titleLabel;
where titleLabel is a custom UILabel that you've initialized and customized.
Check out this Fontful code, for example.
There's no way to do this with the standard title item. You can still add your custom UILabel to the navigation bar and take care of the title changes.
You can not change the font of title.
Instead try out this..
self.title = #"My title is this.";
CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:17.0];
label.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = label;
label.text = self.title;
[label release];

UITableView controller removes custom NavBar TitleView

I have a bit of code that allows me to customize the Navigation Bar title of a view to allow for shrinking / truncating of long titles. The code works fine almost everywhere, however when it's used in a UITableViewController, the title never appears.
The code in question is:
- (void)viewDidLoad
{
[super viewDidLoad];
//Nav bar title for truncating longer titles
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 10.0f;
label.lineBreakMode = UILineBreakModeTailTruncation;
label.text = #"This is a really long title. It will shrink and eventually get truncated correctly.";
self.navigationItem.titleView = label;
}
I've put together a small demo project that illustrates the problem.
https://sites.google.com/site/coffeestainit/files/NavTitleError.zip
I just went through your project. The problem is that in your storyboard, you have not connected your tableViewController to the FirstViewController. So the viewDidLoad of the
FirstViewController is never called.
When I set the custom class of your TableViewController to FirstViewController in the storyboard, it set the label as title, as expected

Why does navigationItem.titleView align left when presentmodalviewcontroller called?

I'm using a UILabel for the titleView of a navigation bar (I'm making simple in-app web browser). It works fine, except that when I present a modal view controller, the titleView shifts from the center of the navbar to the far left (underneath the back button). I've tested in 3.0 and up. Here is relevant code:
- (void)viewDidLoad {
[super viewDidLoad];
// Title view label
CGRect labelFrame = CGRectMake(0.0, 0.0, 120.0, 36.0);
UILabel *label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
label.font = [UIFont boldSystemFontOfSize:14];
label.numberOfLines = 2;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0.0, -1.0);
label.lineBreakMode = UILineBreakModeMiddleTruncation;
self.navigationItem.titleView = label;
}
-(void)displayComposerSheet:(NSString*)mailto
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
}
Screenshots:
Any idea why this is happening? Thanks.
I looked into the problem with some hit and try and found the following facts:
If the UINavigationBar doesn't have the rightBarButtonItem, the titleView shifts towards the right by ~30pts.
It could be reproduced for leftBarButtonItem. But I haven't tried.
In a scenario where the a default UINavigationBar's (with no changes to rightBarButtonItem defaults) titleView is set. And then a new UIView is pushed to the navigation stack which HAS a rightBarButtonItem. Now, if this view is popped [with back button], the navigation bar will remove the rightBarButtonItem. And this will account for the weird offset that shifts the titleView towards a side.
How I fixed the problem was like this:
self.navigationItem.titleView = myCustomTitleView;
// Fake right button to align titleView properly.
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 1)]];
// Width equivalent to system default Done button's (which appears on pushed view in my case).
rightBarButtonItem.enabled = NO;
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
Everything is sweet now. yummmm.
Thanks to DougW for pointing me in right direction. Here's the best hack I found. Basically I retain the UILabel as a class property. Before presenting modal view I unset the titleView, and then reset it immediately after. When the modal view is dismissed I unset then reset the titleView. To the user none of this is visibly notable.
-(void)displayComposerSheet:(NSString*)mailto
{
self.navigationItem.titleView = nil;
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
picker.navigationBar.tintColor = [APPDELEGATE getNavTintColor];
[picker setToRecipients:[NSArray arrayWithObject:mailto]];
[self presentModalViewController:picker animated:YES];
[picker release];
self.navigationItem.titleView = titlelabel;
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
self.navigationItem.titleView = nil;
self.navigationItem.titleView = titlelabel;
[self dismissModalViewControllerAnimated:YES];
}
Does it animate? It may be animating the title view as though it's transitioning to a new view. I don't see anything wrong with your code as written.
I would suggest in your displayComposerSheet, you just unset the titleView, or animate the alpha of the titleView to 0.0. Then, animate it back to 1.0 when you dismiss the modal view controller. Not ideal, but it may look better that way.
Frankly, the whole UINavigation system is crap. We went ahead and re-wrote it ground up because of bizarre issues like these.
The only problem is your frame size. so u have to change it.
Try this one.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 36.0)];
label.font = [UIFont boldSystemFontOfSize:14];
label.numberOfLines = 2;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0.0, -1.0);
label.lineBreakMode = UILineBreakModeMiddleTruncation;
label.text=#"Stack Overflow";
self.navigationItem.titleView = label;
You can try move the code in viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// You code to customize title view
self.navigationItem.titleView = logoImage;
}
It works for me.
If you change the width size to be small like 100 points or smaller instead of 120 you set, this problem may go away. Setting width of the label smaller worked for me.
UIView *view= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[view setUserInteractionEnabled:NO];
view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"logo_small.png"]];
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:view ];
self.navigationItem.leftBarButtonItem = barButton;

Resources