Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setAlpha of UIBarButtonItem is not working in viewDidLoad?

I have a rightBarButtonItem with a button. I changed the alpha of button in viewDidLoad, but it is not come in appearance. Why? if i set this to any action (touch event) its working fine. How to solve this problem and why its not working in ViewDidLoad?

like image 307
Shaheer Palolla Avatar asked Dec 15 '22 09:12

Shaheer Palolla


2 Answers

UIKit automatically resets the alpha value of UIBarButtonItems. To have it disappear you actually need to remove it altogether by setting leftBarButtonItem or rightBarButtonItem to nil. If you just want some transparency you need to have it on an image that you'll set as the barButtonItem background (for this you can use the appearance proxy.

like image 170
Gianluca Tranchedone Avatar answered Dec 17 '22 21:12

Gianluca Tranchedone


It is possible to set the alpha value for a UIBarButtonItem. To do it you need to create a UIButton and then set it as the view of the UIBarButtonItem. Here is an example:

UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pauseButton setTitle:NSLocalizedString(@"pause", nil) forState:UIControlStateNormal];
[pauseButton sizeToFit];
[pauseButton addTarget:self action:@selector(pauseButtonClick) forControlEvents:UIControlEventTouchUpInside];
pauseButton.titleLabel.alpha = 0; /* set this alpha to whatever you like */
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:pauseButton];
like image 43
user3072402 Avatar answered Dec 17 '22 23:12

user3072402