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?
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With