How to set the title of UIToolBar?

Swift 5 version of Rayfleck's great answer:

let titleButton = UIBarButtonItem(title: "My Title", style: .plain, target: nil, action: nil)
titleButton.isEnabled = false
titleButton.setTitleTextAttributes([.foregroundColor : UIColor.black], for: .disabled)

Add this button to the toolbar as normal.


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


I think this would be much cleaner:

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:frame];

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Your Title" 
                                                         style:UIBarButtonItemStylePlain 
                                                        target:nil 
                                                        action:nil];

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

NSArray *items = [[NSArray alloc] initWithObjects:spacer, item, spacer, nil];

[toolbar setItems:items];
toolbar.userInteractionEnabled = NO;

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];