Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a title to a UIToolBar in IOS 7

I have a UIToolBar button declared below:

UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 64.0);

I also have a UIBarButtonItem that gets added in on the left as a back button. But how do I just add a centered label title to the UIToolbar?

like image 253
cdub Avatar asked Oct 21 '13 08:10

cdub


Video Answer


2 Answers

UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 150, 20)];
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
lblTitle.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:lblTitle];
UIBarButtonItem *typeField = [[UIBarButtonItem alloc] initWithCustomView:lblTitle];   
toolBar.items = [NSArray arrayWithArray:[NSArray arrayWithObjects:backButton,spaceBar,lblTitle, nil]];

This will solve your problem i think.

like image 133
Janmenjaya Avatar answered Nov 27 '22 01:11

Janmenjaya


Create a UILabel and add it as an item of the toolbar:

UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:myLabel];

If you want to center it, add flexible spaces on the sides:

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]
like image 41
Antonio MG Avatar answered Nov 27 '22 02:11

Antonio MG