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?
问题:
回答1:
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];
...
}
回答2:
You can do this:
[barButtonItem setTarget:nil];
[barButtonItem setAction:nil];
The button looks enabled but it will not receive any touch event.
回答3:
You can always do:
[yourbutton removeTarget:nil
action:NULL
forControlEvents:UIControlEventAllEvents];
That will remove all actions and targets associated with the button.
回答4:
Make a custom property associated with your button.
Let's say your button fires the action below:
-(IBAction)fireOnButtonPress:(id)sender {
// do something
}
Make an instance variable such as BOOL interactionEnabled;
and in your viewDidLoad or other init method set it to YES
interactionEnabled = YES;
When you need to disable button interaction, simply set it to NO
interactionEnabled = NO;
In your method that is fired when the button is pressed, just add an if-condition checking to see what the state of interactionEnabled
is, like so:
-(IBAction)fireOnButtonPress:(id)sender {
if(interactionEnabled) {
// do something
}
// otherwise ignore button press
}
This won't disable the button, but it will prevent the user from interacting with it when you don't want them to.
回答5:
You alloc-init a custom UILabel as a UIBarbuttonItem (how? see this post) with no text and it should be large enough to cover the UIBarbuttonItem you want to disable. It worked for me.
回答6:
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]
回答7:
You can find your answer here.
In .h File:
IBOutlet UIBarButtonItem *button1;
In .m File:
[button1 setEnabled:FALSE];
Create the UIBarButtonItem as IBOutlet in .h file and access in the implementation file and you can use the problerty of UIBarButtonItem - "setEnabled" to make it enable or disable.
Let me know if you need more help.