I'm trying to use a UIBarButtonItem to put a title on my UIToolbar. I'm using the plain style and that looks fine, but I can't seem to get it to stop highlighting on touch. The Shows Touch When Highlighted option isn't available for the bar button items. Is there a quick and easy way to do this? I'm trying to do the building in interface builder so I can see what I'm doing. I'd prefer not to build the toolbar in the view did load every time.
4 Answers
The property responsible for this is accessible in the UIButton
class:
myButton.showsTouchWhenHighlighted = NO;
You can access this (programmatically) in a UIBarButtonItem by assigning a UIButton to the bar button item's customView
property, and configuring the button. You can do this in Interface Builder too: drag a UIButton onto a UIToolbar, and it will automatically embed it in a UIBarButtonItem for you - then look for the "Shows Touch On Highlight" checkbox under the button's settings.
Incidentally, I don't know how you're customising your buttons so feel free to ignore this, but if your button looks and behaves like a standard toolbar item then users will expect the glow effect.

- 36,683
- 19
- 101
- 139
-
This button itself is just a title. I just need a toolbar so I can have a bunch of buttons, and didn't know any other way to put a title in the middle of it. – codeetcetera May 06 '11 at 20:26
-
2@btate: Ah yes, the title. When I needed a title, I actually just used a standard UILabel with appropriate formatting. Perhaps not as elegant as embedding one in the toolbar as you are doing, but maybe a little simpler. In case you're interested, the formatting for such a label is Helvetica Bold 20.0, white text color, 50% opacity black shadow with -1 vertical offset. – Stuart May 06 '11 at 20:34
I wanted a solution that could be used without any modification to my XIB structure.
The most obvious and simple one worked: subclass UIBarButtonItem
:
UITitleBarButtonItem.h:
//
// UITitleBarButtonItem.m
// Created by Guillaume Cerquant - MacMation on 09/08/12.
//
/*
* A UIBarButtonItem that does not show any highlight on the touch
* Drag and drop a normal UIBarButtonItem in your xib and set its subclass to UITitleBarButtonItem
*/
@interface UITitleBarButtonItem : UIBarButtonItem
@end
UITitleBarButtonItem.m:
#import "UITitleBarButtonItem.h"
@implementation UITitleBarButtonItem
// Only caring about UITitleBarButtonItem set up in Interface Builder. Update this class if you need to instantiate it from code
- (void) awakeFromNib {
UIView *theView = [self valueForKey:@"view"];
if ([theView respondsToSelector:@selector(setUserInteractionEnabled:)]) {
theView.userInteractionEnabled = NO;
}
}
@end
Tested on iOS 5 and the one we aren't allowed to talk yet.

- 21,685
- 6
- 63
- 95
My solution was to set it to disabled, and adjust the titleAttributes
for each UIControlState
let attributes: [NSAttributedStringKey: Any] = [
.font: UIFont.boldSystemFont(ofSize: 16),
.foregroundColor: UIColor.white
]
barButton.setTitleTextAttributes(attributes, for: .enabled)
barButton.setTitleTextAttributes(attributes, for: .disabled)
barButton.isEnabled = false

- 11
- 2
Alternative: 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.

- 1,370
- 9
- 10