ios 按住自定义 UIButton 时更改深灰色突出显示的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15966021/
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 23:09:15  来源:igfitidea点击:

Change colour of dark grey highlight when holding down custom UIButton?

iosobjective-cuibuttontint

提问by Luke

I have a custom UIButtonwhich is a cloud, transparent black and white .png file, with no down state, just one image. When tapping and holding the finger over it, it turns dark grey. I'm trying to change that dark grey to something a little less oppressive. The button is out in the open in a view, not in a tab bar, tool bar, or navigation controller.

我有一个自定义UIButton的云,透明的黑白 .png 文件,没有关闭状态,只有一个图像。用手指轻敲并按住它时,它会变成深灰色。我正在尝试将深灰色更改为不那么压抑的东西。该按钮在视图中处于打开状态,而不是在选项卡栏、工具栏或导航控制器中。

I've already tried setting tintColor(which the documentation helpfully informs me is only suitable for 'some' types of buttons, which no indication as to which).

我已经尝试过设置tintColor(文档有帮助地告诉我它只适用于“某些”类型的按钮,没有说明是什么)。

I've also tried changing everything I can find in Interface Builder relating to highlight colours, default states, etc. Nothing has made a difference at all.

我还尝试更改我在 Interface Builder 中可以找到的与高亮颜色、默认状态等相关的所有内容。根本没有任何区别。

I've even tried setting the button's own image for its UIControlStateHighlightedstate, but even this causes the dark grey overlay to appear when I hold my finger over it.

我什至尝试为按钮的UIControlStateHighlighted状态设置自己的图像,但即使这样也会导致当我将手指放在上面时出现深灰色覆盖层。

How can I change that colour? I've looked at numerous other issues here on SO and have been unable to find a solution that works for me. Any help would be greatly appreciated!

我怎样才能改变这种颜色?我在 SO 上查看了许多其他问题,但一直无法找到适合我的解决方案。任何帮助将不胜感激!

EDIT:I Solved the problem using a category of UIImagewhich adds a method that uses CoreGraphicsto apply a tint to a provided UIImage. I then set THAT image as the highlight, and all is well. Seems a lot of hoop-la to change a colour Apple should've let us change, but c'est la vie.

编辑:我使用一个类别解决了这个问题,该类别UIImage添加了一种CoreGraphics用于将色调应用于提供的UIImage. 然后我将那个图像设置为高光,一切都很好。看起来苹果应该让我们改变颜色的很多箍拉,但是c'est la vie。

回答by Mike Weller

You said you set a custom image for the UIControlStateHighlightedstate. This shoulddisable the default behaviour.

你说你为UIControlStateHighlighted状态设置了一个自定义图像。这应该禁用默认行为。

If you still have problems you can disable this effect by setting the adjustsImageWhenHighlightedproperty to NOand use whatever custom effect you want.

如果您仍然有问题,您可以通过将adjustsImageWhenHighlighted属性设置为NO并使用您想要的任何自定义效果来禁用此效果。

回答by Greg Leszek

If adjustsImageWhenHighlighted = NO is not working, set Button-Type to Custom (IB or programmatically).

如果adjustsImageWhenHighlighted = NO 不起作用,请将Button-Type 设置为Custom(IB 或以编程方式)。

Default Button-Type: System, changes behavior of highlighted button.

默认按钮类型:系统,更改突出显示按钮的行为。

回答by Morten Gustafsson

Swift 3:

斯威夫特 3:

myButton.adjustsImageWhenHighlighted = false

回答by onmyway133

You can write a custom button that does it

您可以编写一个自定义按钮来执行此操作

class ActionButton: UIButton {

  var originalBackgroundColor: UIColor!

  override var backgroundColor: UIColor? {
    didSet {
      if originalBackgroundColor == nil {
        originalBackgroundColor = backgroundColor
      }
    }
  }

  override var isHighlighted: Bool {
    didSet {
      guard let originalBackgroundColor = originalBackgroundColor else {
        return
      }

      backgroundColor = isHighlighted ? originalBackgroundColor.darken() : originalBackgroundColor
    }
  }

回答by Raz

I was having a similar issue with a custom UIButton when the button was highlighting in grey every time it was pressed. I solved that problem by subclassing UIButton and in the implementation I overrode a single method, (void)setHighlighted: method and kept it empty:

当每次按下按钮时按钮都以灰色突出显示时,我遇到了与自定义 UIButton 类似的问题。我通过继承 UIButton 解决了这个问题,并在实现中我覆盖了一个方法, (void)setHighlighted: 方法并将其保持为空:

- (void)setHighlighted:(BOOL)highlighted
{
   // Leave empty to prevent super from doing whatever
   // that it is doing to show the grey highlight.
}

That stopped any type of highlighting as I was not doing anything in the method. It's a better approach if all that you're trying to do is remove any highlighting effect.

这停止了​​任何类型的突出显示,因为我没有在方法中做任何事情。如果您要做的只是消除任何突出显示效果,那么这是一种更好的方法。

So in your code, create a subclass of UIButton, override the setHighlighted method, and then make your custom button a subclass of this custom class.

所以在你的代码中,创建一个 UIButton 的子类,覆盖 setHighlighted 方法,然后让你的自定义按钮成为这个自定义类的子类。