iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iPhone:如何使用buttonType UIButtonTypeCustom设置UIButton的BackgroundColor大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要更改按钮的颜色,当它被点击。我用了 :

[button setBACkgroundColor:[UIColor redColor]];

但这显示红色只有在按钮的四个角落不在整个按钮,当我使用forState:UIControlStateNormal然后应用程序挂起。
是他们的任何方式,点击时在按钮上显示一些颜色?

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

任何帮助将不胜感激。

解决方法

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

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

- (void)setColor:(UIColor *)color forState:(UIControlStatE)state
{
    UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(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:如何使用buttonType UIButtonTypeCustom设置UIButton的BackgroundColor全部内容,希望文章能够帮你解决iPhone:如何使用buttonType UIButtonTypeCustom设置UIButton的BackgroundColor所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。