Alex Rivera | Logout

How to set the title of UIToolBar?

Asked 2010-05-02T18:56:51.320
18

How can I set the title of UIToolBar such that it looks the same as the title in UINavigationBar?

I tried to use a button with plain style, it looks ok, but it will be highlighted when I click on it... Is there any better way to set the title in the detail view of split view?

Edit
Report

4 Answers

27

This is what I use to present a title on a toolbar that will not highlight when pressed:

#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

// choose whatever width you need instead of 600
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 600, 23)];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.shadowColor = UIColorFromRGB(0xe5e7eb);
label.shadowOffset = CGSizeMake(0, 1);
label.textColor = UIColorFromRGB(0x717880);
label.text = @"your title";
label.font = [UIFont boldSystemFontOfSize:20.0];
UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:label];
[label release];
answered 2010-11-02T09:50:09.460
9

This solves the highlighting problem, disabled problem, and touch problem.

Toolbar and titleButton were both created in IB. The view title is covered by the toolbar. So put the title in a toolbar.

self.titleButton.title = @"Order";  // myInterestingTitle

It looks like this: enter image description here

Disable to prevent any highlighting, and to stop it from responding to touches.

self.titleButton.enabled=NO;

Then it looks like this:enter image description here

It will look disabled, so set the color for disabled to white, which has an implicit alpha=1.0. This effectively overrides the 'disabled' look.

[self.titleButton setTitleTextAttributes:
        [NSDictionary dictionaryWithObject:[UIColor whiteColor]
                                    forKey:UITextAttributeTextColor]
                                forState:UIControlStateDisabled ];

Here's what you get: enter image description here

answered 2012-09-20T21:13:55.587
0

Use a UIBarButtonItem in the plain style and additionally cover the toolbar in the appropriate area with a UIView that has a clear background. The view consumes the taps and hides them from the bar button item. Make sure you set the autoresizing mask correctly.

answered 2011-12-13T16:12:13.930
-2

You can uncheck "Shows touch on Highlight" in Interface Builder.

answered 2010-05-02T21:41:37.453

Your Answer