Adding a Uilabel to a Uitoolbar

Adding a UILabel to a UIToolbar

Have a look into this

[[UIBarButtonItem alloc] initWithCustomView:yourCustomView];

Essentially every item must be a "button" but they can be instantiated with any view you require. Here is some example code. Note, since other buttons are typically on the toolbar, spacers are placed on each side of the title button so it stays centered.

NSMutableArray *items = [[self.toolbar items] mutableCopy];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer];
[spacer release];

self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width, 21.0f)];
[self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setTextColor:[UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0]];
[self.titleLabel setText:@"Title"];
[self.titleLabel setTextAlignment:NSTextAlignmentCenter];

UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer2];
[spacer2 release];

UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:self.titleLabel];
[items addObject:title];
[title release];

[self.toolbar setItems:items animated:YES];
[items release];

Add UILabel to UIToolbar Programmatically

EDITED ANSWER

The problem is with the label.
After instanciation, you must draw it with CGRect. so,
add for exemple : label.frame = CGRect(x: 20, y:20, width: 100, height: 50)

Result:

let label = UILabel()
label.text = "Example"
label.frame = CGRect(x: 20, y:20, width: 100, height: 50)

UILabel does not show in UIToolbar

You need to finish setting up the label. You've created a label with no size.

If you want the label to just fit the text you assign to it, call sizeToFit() on the label after setting the label's text.

Setting a title to a UIToolBar in IOS 7

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.

how to set label to center in uitoolbar

By setting page with i am

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (self.view.frame.size.width)-55, 20)];

UILabel on a UIToolbar (programmatically) doesn't display the label

When I run your code it doesn't show either the label or the button. If I make the change below, they both appear:

Change:

self.navigationController.toolbarItems = [NSArray arrayWithObjects:shareButton, spacer1, labelItem, nil];

to:

self.toolbarItems = [NSArray arrayWithObjects:shareButton, spacer1, labelItem, nil];

If you want your label in the middle, make it smaller (I just divided the view width by 2) and add a second spacer on the other side of it:

self.toolbarItems = [NSArray arrayWithObjects:shareButton, spacer1, labelItem, spacer2, nil];

Adding a custom label in toolbar doesn't work

I am sure that this code will help you. Make changes in your code while keeping this code in mind. I have worked on this and it is working fine,

UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:toolBar];

UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width, 21.0f)];
[titleLbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[titleLbl setTextColor=[UIColor blueColor];
[titleLbl setText:@"Title"];
[titleLbl setTextAlignment:UITextAlignmentCenter];

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:titleLbl];
NSArray *toolbarItemArr = [NSArray arrayWithObjects:button, nil];
[toolBar setItems:toolbarItemArr animated:YES];

For better understanding you can follow these links:
UIToolBar Class Reference and UIBarButtonItem Class Reference



Related Topics



Leave a reply



Submit