Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i disable user touch of a UIbarbutton without disabling?

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?

like image 746
Vipin Avatar asked Jun 02 '11 09:06

Vipin


4 Answers

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];
  ...
}
like image 80
Troy Avatar answered Nov 14 '22 23:11

Troy


You can do this:

[barButtonItem setTarget:nil];
[barButtonItem setAction:nil];

The button looks enabled but it will not receive any touch event.

like image 29
bonokite Avatar answered Nov 15 '22 00:11

bonokite


You can always do:

[yourbutton removeTarget:nil 
                  action:NULL 
        forControlEvents:UIControlEventAllEvents]; 

That will remove all actions and targets associated with the button.

like image 45
Thomas Clayson Avatar answered Nov 14 '22 23:11

Thomas Clayson


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]
like image 44
Harikant Amipara Avatar answered Nov 14 '22 23:11

Harikant Amipara