关闭UIBarButtonItem上的突出显示

时间:2011-05-06 19:51:08

标签: ios ipad

我正在尝试使用UIBarButtonItem在我的UIToolbar上添加标题。我使用简单的风格,看起来很好,但我似乎无法让它停止突出显示触摸。 “突出显示时触摸”选项不适用于条形按钮项目。有没有快速简便的方法来做到这一点?我正在尝试在界面构建器中进行构建,以便我可以看到我在做什么。我不希望每次都在视图中构建工具栏。

4 个答案:

答案 0 :(得分:3)

可以在UIButton类:

中访问负责此操作的属性
myButton.showsTouchWhenHighlighted = NO;

您可以通过为条形按钮项的customView属性分配UIButton并配置按钮,在UIBarButtonItem中以编程方式访问它。您也可以在Interface Builder中执行此操作:将UIButton拖到UIToolbar上,它会自动将其嵌入到UIBarButtonItem中 - 然后在按钮的设置下查找“显示触摸突出显示”复选框。

顺便说一下,我不知道你是如何自定义你的按钮所以随意忽略这一点,但如果你的按钮看起来像一个标准的工具栏项,那么用户会期望发光效果。

答案 1 :(得分:2)

我想要一个可以在不对我的XIB结构进行任何修改的情况下使用的解决方案 最明显和最简单的工作:子类UIBarButtonItem

UITitleBarButtonItem.h:

//
//  UITitleBarButtonItem.m
//  Created by Guillaume Cerquant - MacMation on 09/08/12.
//

/*
 * A UIBarButtonItem that does not show any highlight on the touch
 * Drag and drop a normal UIBarButtonItem in your xib and set its subclass to UITitleBarButtonItem
 */
@interface UITitleBarButtonItem : UIBarButtonItem
@end

UITitleBarButtonItem.m:

#import "UITitleBarButtonItem.h"

@implementation UITitleBarButtonItem

// Only caring about UITitleBarButtonItem set up in Interface Builder. Update this class if you need to instantiate it from code

- (void) awakeFromNib {
    UIView *theView = [self valueForKey:@"view"];

    if ([theView respondsToSelector:@selector(setUserInteractionEnabled:)]) {
        theView.userInteractionEnabled = NO;
    }
}

@end

在iOS 5上进行测试,我们还不允许进行对话。

答案 2 :(得分:1)

替代方法:在普通样式中使用UIBarButtonItem,并使用具有清晰背景的UIView覆盖相应区域中的工具栏。视图使用水龙头并将其隐藏在条形按钮项目中。确保正确设置自动调整遮罩。

答案 3 :(得分:0)

我的解决方案是将其设置为已停用,并为每个titleAttributes调整UIControlState

let attributes: [NSAttributedStringKey: Any] = [
    .font: UIFont.boldSystemFont(ofSize: 16),
    .foregroundColor: UIColor.white
]

barButton.setTitleTextAttributes(attributes, for: .enabled)
barButton.setTitleTextAttributes(attributes, for: .disabled)
barButton.isEnabled = false