如果在工具栏中,UIBarButtonItem 在点击时不会突出显示?

发布于 2024-09-19 18:22:28 字数 1128 浏览 3 评论 0原文

我需要导航栏左侧的两个按钮。我想出如何做到这一点的唯一方法是首先将它们放入 UIToolbar 中,然后将 leftBarButtonItem 设置为该值。

如果我这样做,它会正常工作(您可以在点击时看到它突出显示):

UIBarButtonItem* myBtn = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething:)];

self.navigationItem.leftBarButtonItem = myBtn;

但是如果我这样做,按钮操作仍然会发生,但没有突出显示(您点击按钮时没有视觉反馈):

 NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];

 UIBarButtonItem* myBtn = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething:)];

 UIBarButtonItem* myBtn2 = [[UIBarButtonItem alloc] initWithTitle:@"Button2" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomethingElse:)];

 [buttons addObject:myBtn];
 [buttons addObject:myBtn2];

 UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44)];
 [toolbar setItems:buttons animated:NO];
 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];

任何想法为什么这会导致按钮在触摸时不突出显示?

I need two buttons on the left side of a nav bar. The only way I've figured out how to do that is by first putting them into a UIToolbar and then setting the leftBarButtonItem to that.

If I do this it works as normal (you can see it highlight when tapped):

UIBarButtonItem* myBtn = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething:)];

self.navigationItem.leftBarButtonItem = myBtn;

But if I do it like this, the button action still happens but there is no highlight (no visual feedback that you are tapping the button):

 NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];

 UIBarButtonItem* myBtn = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething:)];

 UIBarButtonItem* myBtn2 = [[UIBarButtonItem alloc] initWithTitle:@"Button2" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomethingElse:)];

 [buttons addObject:myBtn];
 [buttons addObject:myBtn2];

 UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44)];
 [toolbar setItems:buttons animated:NO];
 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];

Any idea why this causes the buttons to not highlight when touched?

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

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

发布评论

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

评论(2

瑾兮 2024-09-26 18:22:28

我不认为 UIBarButtonItem 对象在触摸时会突出显示。即使对于导航栏中的默认后退按钮,触摸时也不会突出显示。它只能以这种方式起作用。不确定,但您可以尝试将 UISegmentedControl 与单个段一起使用。它可能会产生突出显示的错觉,并且看起来只是条形按钮。

I dont think the UIBarButtonItem object will be highlighted when touched up. Even for default back button in the navigation bar dosent highlight when touched up. It works in that way only . Not sure but you can try using UISegmentedControl with the single segment . It might create the highlighted illusion and would look like barbutton only.

标点 2024-09-26 18:22:28

在您的选择器函数(例如 doSomething)中,将按钮的 tintColor 属性设置为您想要的突出显示颜色(如果按下),如果未按下则设置为默认颜色。并确保按钮的突出显示颜色与默认颜色不同,以便您知道它已突出显示。

根据 Apple 文档,<代码>showsTouchWhenHighlighted 是:

一个布尔值,用于确定点击按钮是否会使其发光。

但这仅适用于UIButton。我不相信 UIBarButtonItem 有突出显示选项。

请注意,UIBarButtonItem 继承自 UIBarItem,后者又继承自 NSObject,而不是 UIButton,因此您可以期待不同的行为。

In your selector function (e.g. doSomething), set the tintColor property of the button to your desired highlighted color if pressed, and the default if unpressed. And make sure the highlight color of your button is different than the default color so that you know that it is highlighted.

As per the Apple Docs, the function of showsTouchWhenHighlighted is:

A Boolean value that determines whether tapping the button causes it to glow.

but this is only for UIButton. I don't believe UIBarButtonItem has a highlight option.

Note that UIBarButtonItem inherits from UIBarItem which inherits from NSObject, not UIButton, so you can expect different behavior.

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