xcode 如何在不禁用的情况下禁用用户对 UIbarbutton 的触摸?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6212827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i disable user touch of a UIbarbutton without disabling?
提问by Christina
i have a UiBarButton item in my Toolbar.i need to deactivate the user touch interaction in UiBarButton. there is no setUserInteractionEnabledproperty 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?
我的工具栏中有一个 UiBarButton 项目。我需要停用 UiBarButton 中的用户触摸交互。它没有setUserInteractionEnabled属性。当我隐藏它时,没有适当的可见性。谁能告诉我如何禁用 UIbarbutton 的用户触摸交互而不禁用它?
回答by Troy
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.
要在 UIToolBar 中有标题,请将 UIBarButtonItem 添加到您的工具栏,然后将其 customView 属性设置为 UILabel。然后您可以设置标签的文本并且没有任何突出显示等。
// 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];
...
}
回答by bonokite
You can do this:
你可以这样做:
[barButtonItem setTarget:nil];
[barButtonItem setAction:nil];
The button looks enabled but it will not receive any touch event.
该按钮看起来已启用,但它不会收到任何触摸事件。
回答by Thomas Clayson
You can always do:
你总是可以这样做:
[yourbutton removeTarget:nil
action:NULL
forControlEvents:UIControlEventAllEvents];
That will remove all actions and targets associated with the button.
这将删除与按钮关联的所有操作和目标。
回答by aqua
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
创建一个实例变量,例如BOOL interactionEnabled;
和 在您的 viewDidLoad 或其他 init 方法中将其设置为YES
interactionEnabled = YES;
When you need to disable button interaction, simply set it to NO
当您需要禁用按钮交互时,只需将其设置为 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:
在按下按钮时触发的方法中,只需添加一个 if 条件检查以查看状态interactionEnabled
是什么,如下所示:
-(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.
这不会禁用按钮,但会阻止用户在您不希望他们与之交互时与其进行交互。
回答by acecapades
回答by Harikant Amipara
Obj - c
对象-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
斯威夫特 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]
回答by AppAspect
You can find your answer here.
你可以在这里找到你的答案。
In .h File:
在 .h 文件中:
IBOutlet UIBarButtonItem *button1;
In .m File:
在 .m 文件中:
[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.
在 .h 文件中创建 UIBarButtonItem 作为 IBOutlet 并在实现文件中访问,您可以使用 UIBarButtonItem 的问题 - “setEnabled”来启用或禁用它。
Let me know if you need more help.
如果您需要更多帮助,请告诉我。