gpt4 book ai didi

iPhone : How to set BackgroundColor of UIButton with buttonType UIButtonTypeCustom

转载 作者:太空狗 更新时间:2023-10-30 03:08:44 24 4
gpt4 key购买 nike

我想在单击时更改按钮颜色。我用过:

[button setBackgroundColor:[UIColor redColor]];

但这只在按钮的四个角上显示红色,而不是在整个按钮上显示红色,而且当我使用 forState:UIControlStateNormal 时应用程序挂起。单击时他们有什么方法可以在按钮上显示一些颜色吗?

[click1 setBackgroundColor:[UIColor redColor] forState:UIControlStateHighlighted];

我们将不胜感激。

最佳答案

您可以以编程方式创建图像,因此能够以动态方式使用您的颜色:

使用此方法为 UIButton 创建一个类别,并确保通过 @import QuartzCore 导入 QuartzCore 库:

- (void)setColor:(UIColor *)color forState:(UIControlState)state
{
UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
colorView.backgroundColor = color;

UIGraphicsBeginImageContext(colorView.bounds.size);
[colorView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[self setBackgroundImage:colorImage forState:state];
}

这将创建一个图像,该图像具有您指定颜色的按钮大小,然后将其分配给所需的状态。由于使用了 backgroundImage,您仍然可以通过 setTitle:forState: 方法为按钮设置标题。

关于iPhone : How to set BackgroundColor of UIButton with buttonType UIButtonTypeCustom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4049103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com