i have a UiBarButton item in my Toolbar.i need to deactivate the user touch interaction in UiBarButton. there is no setUserInteractionEnabled property to it. when i am hiding it there is no proper visibility .can any one tell me that how can i disable user touch interaction of a UIbarbutton without disabling it?
To have a title in a UIToolBar, add a UIBarButtonItem to your toolbar and then set its customView property to a UILabel. You can then set the text of the label and not have any highlighting, etc.
// In @interface section:
@property (weak, nonatomic) IBOutlet UIBarButtonItem *titleButtonItem;
// In @implementation section:
- (void)viewDidLoad {
...
UILabel *titleLabel = [[UILabel alloc] init];
self.titleButtonItem.customView = titleLabel;
titleLabel.text = @"Some Title Text";
[titleLabel sizeToFit];
...
}
You can do this:
[barButtonItem setTarget:nil];
[barButtonItem setAction:nil];
The button looks enabled but it will not receive any touch event.
You can always do:
[yourbutton removeTarget:nil
action:NULL
forControlEvents:UIControlEventAllEvents];
That will remove all actions and targets associated with the button.
Obj - c
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barButton setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[barButton setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateDisabled];
barButton.frame = CGRectMake(10.0,10.0,50,50);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
backButton.enabled = NO;
self.navigationItem.leftBarButtonItem = backButton;
Swift 4
For Button Image
let barButton = UIButton(type: .custom)
barButton.setImage(UIImage(named: "image.png"), for: .normal)
barButton.setImage(UIImage(named: "image.png"), for: .disabled)
barButton.frame = CGRect(x: 10.0, y: 10.0, width: 50, height: 50)
let backButton = UIBarButtonItem(customView: aButton)
backButton.isEnabled = false
navigationItem.leftBarButtonItem = backButton
For Button Title
let btnTitle = UIBarButtonItem(title: "Your Button title", style: .plain, target: nil, action:nil)
btnTitle.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "fontname", size: 14.0)!], for: .normal)
btnTitle.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "fontname", size: 14.0)!], for: .disabled)
btnTitle.isEnabled = false
self.navigationItem.leftBarButtonItems = [btnTitle]
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