Turn off backlight on UIBarButtonItem

I am trying to use a UIBarButtonItem to put a title on my UIToolbar. I use a simple style and it looks great, but I can't make it stop highlighting the touch. The option "Show touch when selection" is not available for panel elements. Is there a quick and easy way to do this? I am trying to make a building in an interface builder so that I can see what I am doing. I would prefer not to create a toolbar in the view, loaded every time.

+6
source share
4 answers

The property responsible for this is available in the UIButton class:

 myButton.showsTouchWhenHighlighted = NO; 

You can access this (programmatically) in the UIBarButtonItem by assigning a UIButton element to the customView button customView and clicking the button. You can also do this in Interface Builder: drag a UIButton onto a UIToolbar and it will automatically add it to the UIBarButtonItem for you - then find the “Shows Touch On Highlight” checkbox under the button settings.

By the way, I don’t know how you customize your buttons, so feel free to ignore this, but if your button looks and behaves like a standard toolbar item, users expect a glow effect.

+3
source

I need a solution that could be used without any changes to my XIB structure.
The most obvious and simple job: a subclass of 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 we are not yet allowed to talk.

+2
source

Alternative: use the UIBarButtonItem in the usual style and optionally close the toolbar in the corresponding area with the UIView, which has a clear background. The view consumes taps and hides them from the panel button element. Make sure that you have correctly configured the autoresist mask.

+1
source

My solution was to disable it and set 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 
0
source

Source: https://habr.com/ru/post/887478/


All Articles