ios 如何更改 UIButton 的突出显示颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20300766/
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
How to change the highlighted color of a UIButton?
提问by Code Monkey
I created a navigation button in my UINavigationController. I set it to be highlighted when touched:
我在 UINavigationController 中创建了一个导航按钮。我将它设置为在触摸时突出显示:
[someButton setShowsTouchWhenHighlighted:YES];
Is there a way to change the highlighted color to something other than the default white?
有没有办法将突出显示的颜色更改为默认白色以外的颜色?
回答by User 1531343
Try to Override the UIButton with the following Method.. and just change the backgroud color of button when its in highlighted state.
尝试使用以下方法覆盖 UIButton .. 并在按钮处于突出显示状态时更改按钮的背景颜色。
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.backgroundColor = [UIColor Your Customcolor];
}
else{
self.backgroundColor = [UIColor Your DefaultColor];
}
}
Try it..hope it helps
试试吧..希望它有帮助
回答by bradley
You can use setBackgroundImage:forState:
to set the background image for the button when highlighted.
您可以使用setBackgroundImage:forState:
设置突出显示时按钮的背景图像。
ex:
前任:
As suggested by Tim in their answer here, you can create aUIImage
from UIColor
:
正如 Tim 在他们的回答中所建议的,您可以UIImage
从UIColor
以下位置创建一个:
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Then set image as button's background image when highlighted
然后在突出显示时将图像设置为按钮的背景图像
[button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateHighlighted];
[button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateHighlighted];
回答by King-Wizard
In Swift:
在斯威夫特:
import UIKit
class CustomUIButtonForUIToolbar: UIButton {
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
super.drawRect(rect)
self.layer.borderColor = UIColor.blueColor().CGColor
self.layer.borderWidth = 1.0
self.layer.cornerRadius = 5.0
self.clipsToBounds = true
self.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
self.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
}
override var highlighted: Bool {
didSet {
if (highlighted) {
self.backgroundColor = UIColor.blueColor()
}
else {
self.backgroundColor = UIColor.clearColor()
}
}
}
}
回答by ArVID220u
I'll provide a Swiftanswer, which can be used without any customization, if you want the highlighted color to be a darkened version of the original background color. If you, on the other hand, want a completely different highlighted background color, you can supply that to the highlightedBackgroundColor property as a UIColor.
如果您希望突出显示的颜色是原始背景颜色的变暗版本,我将提供一个Swift答案,无需任何自定义即可使用。另一方面,如果您想要一个完全不同的高亮背景颜色,您可以将其作为 UIColor 提供给 highlightBackgroundColor 属性。
The following is the most efficient and straightforward way to implement such a functionality in Swift. It is also generic, meaning it can be used for lots of different buttons with different colors.
以下是在 Swift 中实现此类功能的最有效和最直接的方法。它也是通用的,这意味着它可以用于许多不同颜色的不同按钮。
Here's the code:
这是代码:
import UIKit
class HighlightedColorButton: UIButton {
// A new highlightedBackgroundColor, which shows on tap
var highlightedBackgroundColor: UIColor?
// A temporary background color property, which stores the original color while the button is highlighted
var temporaryBackgroundColor: UIColor?
// Darken a color
func darkenColor(color: UIColor) -> UIColor {
var red = CGFloat(), green = CGFloat(), blue = CGFloat(), alpha = CGFloat()
color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
red = max(red - 0.5, 0.0)
green = max(green - 0.5, 0.0)
blue = max(blue - 0.5, 0.0)
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
// Set up a property observer for the highlighted property, so the color can be changed
@objc override var highlighted: Bool {
didSet {
if highlighted {
if temporaryBackgroundColor == nil {
if backgroundColor != nil {
if let highlightedColor = highlightedBackgroundColor {
temporaryBackgroundColor = backgroundColor
backgroundColor = highlightedColor
} else {
temporaryBackgroundColor = backgroundColor
backgroundColor = darkenColor(temporaryBackgroundColor!)
}
}
}
} else {
if let temporaryColor = temporaryBackgroundColor {
backgroundColor = temporaryColor
temporaryBackgroundColor = nil
}
}
}
}
}
Treat the button as a normal UIButton, with the addition of the optional property highlightedBackgroundColor:
将按钮视为普通的 UIButton,并添加了可选属性 highlightBackgroundColor:
let highlightedColorButton = HighlightedColorButton.buttonWithType(.Custom) as HighlightedColorButton
highlightedColorButton.backgroundColor = UIColor.redColor()
highlightedColorButton.highlightedBackgroundColor = UIColor.blueColor()
回答by Vinay Jain
Use this statement to set the highlighted color of the UIButton:
使用此语句设置 UIButton 的突出显示颜色:
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
回答by CodeOverRide
In Swift, if you want to use a image for highlighted state, you can do this with UIButton
:
在 Swift 中,如果您想将图像用于突出显示状态,您可以使用以下命令UIButton
:
if enabled {
//highlighted state
enableMicrophone.setImage(UIImage(named: "mic_on"), forState: .Highlighted)
} else {
//normal state
enableMicrophone.setImage(UIImage(named: "mic_off"), forState: .Normal)
}
回答by Yukito Shibuya
For Swift
对于斯威夫特
import UIKit
extension UIImage {
func imageWithAlphaComponent(alpha: CGFloat) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, 0)
let ctx = UIGraphicsGetCurrentContext()
let area = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
CGContextScaleCTM(ctx, 1, -1)
CGContextTranslateCTM(ctx, 0, -area.size.height)
CGContextSetBlendMode(ctx, CGBlendMode.Multiply)
CGContextSetAlpha(ctx, alpha)
CGContextDrawImage(ctx, area, self.CGImage)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
Write
写
button.setImage(image.imageWithAlphaComponent(0.65), forState: .Highlighted)
https://gist.github.com/yukitoto/e8aa420e20be7ab5d64b71e96ecd61f1
https://gist.github.com/yukitoto/e8aa420e20be7ab5d64b71e96ecd61f1
回答by A Fader Darkly
I'm getting some success by using an old, old web trick.
我通过使用旧的网络技巧取得了一些成功。
I've created a couple of image assets in images.xcassets
, PNG files, both 1px by 1px. One is the normal colour, the other is the background colour.
我在images.xcassets
PNG 文件中创建了几个图像资产,都是 1px x 1px。一种是正常颜色,另一种是背景颜色。
I'm going from pure black to pure white, so:
我要从纯黑色变成纯白色,所以:
In the storyboard I select the button. In the 'Attributes inspector' (right-hand panel, 4th icon across) I change the type to 'custom', change the 'state config' to 'highlight', then 'background' to 'whitePixel' (or whatever you called it) and 'Text Color' to 'black color'.
在故事板中,我选择了按钮。在“属性检查器”(右侧面板,第 4 个图标)中,我将类型更改为“自定义”,将“状态配置”更改为“突出显示”,然后将“背景”更改为“whitePixel”(或您称之为的任何名称) ) 和“文本颜色”到“黑色”。
Now I change the 'state config' to 'Default', the 'background' to 'blackPixel' (or whatever) and the 'Text Color' to 'white color'.
现在我将“状态配置”更改为“默认”,将“背景”更改为“blackPixel”(或其他),将“文本颜色”更改为“白色”。
I guess you can use these pixels whenever you need to apply a colour when you only have access to images in the Storyboard editor.
我想当您只能访问 Storyboard 编辑器中的图像时,您可以在需要应用颜色时使用这些像素。
This does increase the number of image assets, but reduces the required code hugely. Which pleases me.
这确实增加了图像资产的数量,但大大减少了所需的代码。这让我高兴。