给UIButton设置BackgroundColor:forState
来源:互联网 发布:牟宗三 知乎 编辑:程序博客网 时间:2024/06/08 09:54
洲洲哥在写这篇文章的时候,我想起了之前写过的一篇文章
《 iOS给UIButton添加一个新的属性》,大家在读这篇文章前可以先复习一下哦。。。。
今天带来的就是利用延展做一个偷懒的事情。。
在开发过程中要设置一个uibutton的背景颜色并且要分当前按钮的状态的时候。那写的就是相当费劲的。不过哥吃饱了。这种费力讨好的事情 就让洲洲哥代劳吧!!!
废话不多说,上代码。。。<各位此处应该有笑声,,如果有请持续5秒钟>
先看看 利用这个延展的调用代码:
[self.itemBtn setBackgroundColor:YSColor(254, 245, 245) forState:UIControlStateNormal];[self.itemBtn setBackgroundColor:YSColor(252, 94, 94) forState:UIControlStateSelected];
和我们平常设置button的时候一样,so easy
[self.itemBtn setTitleColor:YSColor(110, 110, 110) forState:UIControlStateNormal]; [self.itemBtn setTitleColor:YSColor(255, 255, 255) forState:UIControlStateSelected];下面开始展示代码咯
添加一个延展名字当然是对uibutton的延展咯(这里同时也对button添加了一个titleNmae的属性)
首先看.h文件的写法
#import <UIKit/UIKit.h>@interface UIButton (FillColor)- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state;@property (nonatomic, strong) NSString * titleName;@end
下面是.m的写法
#import "UIButton+FillColor.h"#import "objc/runtime.h"static const void * titleNameBy = &titleNameBy;@implementation UIButton (FillColor)@dynamic titleName;// 添加自定义属性-(void)setTitleName:(NSString *)titleName { objc_setAssociatedObject(self, titleNameBy, titleName, OBJC_ASSOCIATION_COPY_NONATOMIC);}-(NSString *)titleName { return objc_getAssociatedObject(self, titleNameBy);}// 设置背景颜色for state- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state { [self setBackgroundImage:[UIButton imageWithColor:backgroundColor] forState:state];}// 设置颜色+ (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;}@end
上面就是咯,这次装逼到此结束。掌声吧各位。
提高更多装逼能力,请关注我的微信公众号,不定期发送洲洲哥在工作中遇见问题的解决方法和干货哦
0 0
- 给UIButton设置BackgroundColor:forState
- UIButton setBackgroundColor:ForState
- UIButton实现setBackgroundColor:ForState功能
- UIButton的setImage:forstate:adjustsImageWhenDisabled
- UIButton的setImage:forState:和setBackgroundImage:forState:方法
- 给只有 backgroundColor 属性的类设置背景图片
- 给UIButton设置一个背景图片
- SDWebImage [UIButton setImageWithURL:(NSURL *) forState:(UIControlState)] 的bug
- iOS给UIButton设置圆角
- 关于StoryBord 中给UIButton 设置边框
- activity 获取 rootView 设置 backGroundColor
- backgroundcolor
- 设置按钮等文本,得用[Button setTitleColor:textColor forState:UIControlStateNormal]
- iOS --- 为UIButton添加setBackgroundColor:forState:方法(包含OC和Swift两个版本)
- backgroundColor的背景颜色alpha透明度设置
- iOS--UIWebView设置backgroundColor无效问题
- UIButton在设置Image时,会把之前设置的frame给覆盖掉。
- UITableViewCell通过cell.backgroundColor设置背景颜色为什么没有效果
- 帧和场
- 拦截导弹
- Guava教程
- Java数据抓取总结--2.使用Jsoup抓取当前天气情况
- 21.leetcode题目234: Palindrome Linked List(第2种方法是链表翻转,等做完206题再做!)
- 给UIButton设置BackgroundColor:forState
- oracle中的exists 和not exists 用法详解 (和in)
- 简单的显式和隐式启动
- Win10+Visual Studio 2015Proofessional+opencv3.0完美配置
- LeetCode 202 Happy number
- ImageContainer重复添加,以及Map<String,Object>遍历
- ArcGIS调用DataInteroperabilityTools导出数据
- bzoj1015 星球大战starwar
- 在tomcat7中启用HTTPS的详细配置