SetAlpha from UIBarButtonItem not working in viewDidLoad?

I have a rightBarButtonItem with a button. I changed the alpha of the button in viewDidLoad, but it did not appear. What for? if I set it for any action (touch event) its performance. How to solve this problem and why it does not work in ViewDidLoad?

+4
source share
3 answers

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

+6
source

You can set the alpha value for UIBarButtonItem. To do this, you need to create a UIButton and then set it as a UIBarButtonItem view. 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]; 
+5
source

UIKit automatically sets the alpha of the customView UIBarButtonItem , but you can override and apply the required alpha using your own class. (If you want a translucent button).

 // For use in UIBarButtonItem - UIKit resets the alpha of custom views automatically final class HalfTransparentButton: UIButton { override var alpha: CGFloat { set { super.alpha = 0.5 } get { return 0.5 } } } 
+1
source

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


All Articles