ios 如何删除 UIButton 选定的背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8790576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:11:44  来源:igfitidea点击:

How to remove UIButton selected background color?

iosobjective-ciphoneuibuttonuikit

提问by TommyG

I am looking for a solution to this problem that does NOT require using images/PNGs. I am looking for a way to remove the UIButton's blue background color when it's in selected state, and I just cannot find a way to do that without using images. I am basically trying to do something like that in case of a UITableViewCell:

我正在寻找不需要使用图像/PNG 的解决方案。我正在寻找一种方法来UIButton在它处于选定状态时删除的蓝色背景颜色,但我找不到不使用图像的方法。我基本上是在尝试做类似的事情,以防万一UITableViewCell

   cell.selectionStyle = UITableViewCellSelectionStyleNone;

采纳答案by sudo rm -rf

Unfortunately I'm away from my test machine at the moment, but there are two things you could try.

不幸的是,我目前不在我的测试机旁,但是您可以尝试两件事。

First would be to set the following property:

首先是设置以下属性:

button.adjustsImageWhenHighlighted = NO;

Or uncheck "Highlight adjusts image" in Interface Builder.

或者在 Interface Builder 中取消选中“突出显示调整图像”。

If that doesn't work like you expect it to, you can deselect the button in the action you tied it to, like so:

如果这不像您期望的那样工作,您可以在绑定它的操作中取消选择按钮,如下所示:

- (IBAction)yourButtonAction:(id)sender {
    [sender setHighlighted:NO];
}

回答by Rufus

I had this same issue and I resolve it by changing the UIButton type from "System" to "Custom". The blue background color does not show up in selected state when it is a "Custom" type.

我遇到了同样的问题,我通过将 UIButton 类型从“系统”更改为“自定义”来解决它。当它是“自定义”类型时,蓝色背景颜色不会在选定状态下显示。

回答by Ali Seymen

Change the alpha of the tintColor to zero, works if iOS is 5.0 or later

将 tintColor 的 alpha 更改为零,适用于 iOS 5.0 或更高版本

UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];  
button.tintColor = color;

回答by Akash Bhardwaj

Very simple, in storyBoard, select your UIButtonand open attribute inspector. Now scroll down to Viewsection and change Tintproperty to Clear Coloror any specific color if u want.

很简单,在storyBoard,选择你的UIButton并打开attribute inspector。现在向下滚动到View部分并根据需要将Tint属性更改为Clear Color或任何特定颜色。

enter image description here

在此处输入图片说明

回答by Hyman

UIButton also with customtype, we can set it in xcodeas-

UIButton 也有custom类型,我们可以将它设置为xcode-

enter image description here

在此处输入图片说明

ORBy programmatically as-

以编程方式作为-

 let customButton = UIButton(type: .custom) 
  customButton.setTitle("this is Button", for: .normal)
  customButton.setTitleColor(UIColor.blue, for: .normal)
  customButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500)
            self.view.addSubview( customButton)

Now there is no more UIButton's blue background color

现在不再有UIButton 的蓝色背景色了

回答by Leon

Not a direct answer to OP's question but to remove this colour in a UIButtonsubclass you can do this:

不是对 OP 问题的直接回答,而是要在UIButton子类中删除此颜色,您可以执行以下操作:

override func layoutSubviews() {
    tintColor = .clear
    super.layoutSubviews()
}

回答by Sasa Blagojevic

What did it for me was changing the button.tintColor in the highlighted state. This is swift 3, iOS 10

对我来说是什么改变了突出显示状态的 button.tintColor。这是 swift 3, iOS 10

override public var isHighlighted: Bool {
    didSet {
        self.tintColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0)
    }
}

Or you can do the same in the interface builder

或者您可以在界面构建器中执行相同操作

Choose button state

选择按钮状态

Choose button tint color

选择按钮色调颜色

回答by jk2K

Swift Version

迅捷版

extension UIButton {
    override public var highlighted: Bool {
        didSet {
            // clear background color when selected
            self.layer.backgroundColor = .None
        }
    }
}

回答by JDx

Not sure if this is the best way. But you could have an invisible button or view ontop of the actual button then recognize when the top button/view has been clicked.

不确定这是否是最好的方法。但是您可以在实际按钮的顶部有一个不可见的按钮或视图,然后识别何时单击了顶部按钮/视图。

回答by Simon Lee

Use...

用...

    [myButton setAdjustsImageWhenHighlighted:FALSE];