Programmatically Highlight Uibarbuttonitem

programmatically highlight UIBarButtonItem

setSelected and setHighlighted work fine on UIControls, but not UIBarButtonItems (which are not UIControls).

I'd recommend using UIBarButtonItem's - (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics (documentation linked) method to change the background image to something that mimics highlighting.

You can also set a custom UIView on the item which also mimics highlighting (see the customView property).

UIBarButtonItem in navigation bar is highlighted when selected but UIBarButtonItem in toolbar is not

You simply misconfigured the UIbarButtonItem/UIButton in Interface Builder's Attributes Inspector.

There is no point to waste time investigating such a trivial issue, no matter how puzzling it may seem. Just delete that Start button and drop a new one into the toolbar again.
There is no reason for it to behave differently that the options button above.

UIBarButtonItem will be always highlight when I click it

Is it a bug in iOS 11.2?

Yes. There's an iOS 11 bug with the right bar button item in the root view controller. When you push to the next view controller and pop back, the right bar button item is dimmed.

That is the bug seen in your screencast. In your code, you set the right bar button item's tint color to white. And initially, it is white. But when you push and then pop, it is no longer white.

What I do is work around this in the view controller's viewWillAppear, as follows:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.tintAdjustmentMode = .normal
self.navigationController?.navigationBar.tintAdjustmentMode = .automatic
}

Turn off highlighting on UIBarButtonItem

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.

How to fire hightlight effect on UIBarButtonItem

The following method assumes you are using the toolbarItems property of a navigation controller and that the button you want to highlight is the last item in that array. You can apply it to any toolbar, of course, and pick a different array index if necessary.

Right now this is not exactly perfect, because the animation moves the new button from the lower left of the screen. However, I think that can be turned off with a little effort.

Note that I used PeakJi's image.

I hope this works for you.

Enjoy,

Damien

- (void)highlightButton {
NSArray *originalFooterItems = self.toolbarItems;
NSMutableArray *footerItems = [originalFooterItems mutableCopy];
[footerItems removeLastObject];

NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:@"white_glow" ofType:@"png"];
UIImage* anImage = [UIImage imageWithContentsOfFile:pathToImageFile];
UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];
imageView.frame = CGRectMake(0, 0, 40, 40);

UIBarButtonItem *flashImage = [[UIBarButtonItem alloc] initWithCustomView:imageView];
[footerItems addObject:flashImage];

[UIView animateWithDuration:0.3
delay: 0.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
self.toolbarItems = footerItems; }
completion:^(BOOL finished){
[UIView animateWithDuration:.3
delay: 0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.toolbarItems = originalFooterItems;
}
completion:nil];
}];
}

Globally change UIBarButtonItem text color when selected or highlighted

You have your didFinishLaunchingWithOptions function in your AppDelegate witch tells the delegate that the launch process is almost done and the app is almost ready to run.

And in there you can use the appearance on your UIBarButtonItem and UINavigationBar.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Text
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGray], for: .selected)

// Images
UINavigationBar.appearance().tintColor = UIColor.orange

return true
}

Result:
Sample Image

Adding a UIBarButtonItem programmatically

you are adding UIBarButtonItem to a new instance of NavigationController. so it will not appear there.

so in your view controller which you want to handle right navigation bar , under one of these methods:
override func viewDidLoad()
or
override func viewWillAppear

add:

let rightBarButton = UIBarButtonItem(title: "LogIn", style: .plain, target: self, action: #selector(logInPressed))

self.navigationController?.navigationItem.setRightBarButton(rightBarButton, animated: true)


Related Topics



Leave a reply



Submit