如果在工具栏中,UIBarButtonItem 在点击时不会突出显示?
我需要导航栏左侧的两个按钮。我想出如何做到这一点的唯一方法是首先将它们放入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为 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.
在您的选择器函数(例如
doSomething
)中,将按钮的tintColor
属性设置为您想要的突出显示颜色(如果按下),如果未按下则设置为默认颜色。并确保按钮的突出显示颜色与默认颜色不同,以便您知道它已突出显示。根据 Apple 文档,<代码>showsTouchWhenHighlighted 是:
但这仅适用于
UIButton
。我不相信UIBarButtonItem
有突出显示选项。请注意,
UIBarButtonItem
继承自UIBarItem
,后者又继承自NSObject
,而不是UIButton
,因此您可以期待不同的行为。In your selector function (e.g.
doSomething
), set thetintColor
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:but this is only for
UIButton
. I don't believeUIBarButtonItem
has a highlight option.Note that
UIBarButtonItem
inherits fromUIBarItem
which inherits fromNSObject
, notUIButton
, so you can expect different behavior.