views:

255

answers:

1

I am trying to put a label in a UIBarButtonItem. Here is my viewDidLoad method:

characterCountLabel = [[UILabel alloc] init];
characterCountLabel.text = @"HELLO";
charCountButton = [[UIBarButtonItem alloc]initWithCustomView:characterCountLabel];

Everything is hooked up properly in IB. Why is the label not appearing inside of my UIBarButtonItem?

A: 

A couple things come to mind. First, your label has no frame. Try using

characterCountLabel = [[UILabel alloc] initWithFrame:
                                 CGRectMake(0.0f, 0.0, 200.0f, 25.0f)];

Next, can you show us the code where you actually add the bar button item? What are you adding it to--a navigation bar or a toolbar?

When you say everything is hooked up in IB, what are you referring to? The toolbar? Navigation bar? Are you using a navigation controller? If not, then you should be able to just add an item to a navigation bar in IB.

A little more information would help us help you.

Matt Long
i have a UIToolBar in IB. I added a UIBarButtonItem to the toolbar in IB
Sheehan Alam
So you want to add a UILabel to the UIBarButtonItem you created in IB? I don't think there is any way to set the custom view parameter on the UIBarButtonItem once it's created. It is only done in the init, so if you have an item your are connecting in IB to the charCountButton, you may have problems. I would disconnect that and do it all in code.
Matt Long