iPhone:如何使用buttonType UIButtonTypeCustom设置UIButton的BackgroundColor

iPhone:如何使用buttonType UIButtonTypeCustom设置UIButton的BackgroundColor,第1张

概述我想要更改按钮的颜色,当它被点击。我用了 : [button setBackgroundColor:[UIColor redColor]]; 但这显示红色只有在按钮的四个角落不在整个按钮,当我使用forState:UIControlStateNormal然后应用程序挂起。 是他们的任何方式,点击时在按钮上显示一些颜色? [click1 setBackgroundColor:[UIColor red 我想要更改按钮的颜色,当它被点击。我用了 :

[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所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1087576.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存