【问题标题】:UIBarButtonItem doesn't highlight when tapped if in a Toolbar?如果在工具栏中点击,UIBarButtonItem 不会突出显示?
【发布时间】:2010-09-17 00:52:42
【问题描述】:

我需要导航栏左侧的两个按钮。我想出如何做到这一点的唯一方法是首先将它们放入 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];

知道为什么这会导致按钮在触摸时不突出显示吗?

【问题讨论】:

    标签: iphone uinavigationbar uitoolbar uinavigationitem


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      根据Apple DocsshowsTouchWhenHighlighted的作用是:

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

      但这仅适用于UIButton。我不相信UIBarButtonItem 有高亮选项。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多