如何在不禁用的情况下禁用用户触摸 UIbarbutton?

发布于 2024-11-13 10:21:41 字数 165 浏览 5 评论 0原文

我的工具栏中有一个 UiBarButton 项目。我需要停用用户触摸交互 UiBarButton。它没有 setUserInteractionEnabled 属性。当我隐藏它时,没有适当的可见性。有人可以告诉我如何在不禁用 UIbarbutton 的情况下禁用它的用户触摸交互吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浅黛梨妆こ 2024-11-20 10:21:41

要在 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];
  ...
}

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];
  ...
}
完美的未来在梦里 2024-11-20 10:21:41

您可以这样做:

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

按钮看起来已启用,但不会接收任何触摸事件。

You can do this:

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

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

谢绝鈎搭 2024-11-20 10:21:41

您始终可以这样做:

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

这将删除与该按钮关联的所有操作和目标。

You can always do:

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

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

迷途知返 2024-11-20 10:21:41

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

按钮图像

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

按钮标题

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]

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]
遥远的绿洲 2024-11-20 10:21:41

创建与您的按钮关联的自定义属性。

假设您的按钮触发以下操作:

-(IBAction)fireOnButtonPress:(id)sender {
    // do something
}

创建一个实例变量,例如 BOOL InteractionEnabled; 并在 viewDidLoad 或其他 init 方法中将其设置为 YES

interactionEnabled = YES;

当您需要禁用按钮时交互,只需将其设置为 NO

interactionEnabled = NO;

在按下按钮时触发的方法中,只需添加一个 if 条件检查即可查看 interactionEnabled 的状态,例如所以:

-(IBAction)fireOnButtonPress:(id)sender {
    if(interactionEnabled) {
        // do something
    }
    // otherwise ignore button press
}

这不会禁用该按钮,但是当您不希望用户与该按钮交互时,它会阻止用户与其交互。

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.

零度℉ 2024-11-20 10:21:41

您将自定义 UILabel 分配初始化为 UIBarbuttonItem (如何?请参阅此 帖子),没有文本,它应该足够大以覆盖您要禁用的 UIBarbuttonItem。这对我有用。

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.

睡美人的小仙女 2024-11-20 10:21:41

您可以在这里找到答案。

在.h文件中:

IBOutlet UIBarButtonItem *button1;

在.m文件中:

[button1 setEnabled:FALSE];

在.h文件中创建UIBarButtonItem作为IBOutlet并在实现文件中访问,您可以使用UIBarButtonItem的问题 - “setEnabled”来启用或禁用它。

如果您需要更多帮助,请告诉我。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文