UIButton+BackgroundColor.h

#import <UIKit/UIKit.h>

@interface UIButton (BackgroundColor)

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state;
@end

UIButton+BackgroundColor.m

#import "UIButton+BackgroundColor.h"

@implementation UIButton (BackgroundColor)

- (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

iOS-UIButton分类扩展(封装)的更多相关文章

  1. IOS中类的扩展(协议,分类)

    IOS中类的扩展(协议,分类) 扩展类,我们可以使用协议和分类这两种方法,下面我们来分别实现这两种方法: 参考网址:http://www.cnblogs.com/wendingding/p/37095 ...

  2. 开源 iOS 项目分类索引大全 - 待整理

    开源 iOS 项目分类索引大全 GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Cate ...

  3. iOS 通知中心扩展制作初步-b

    涉及的 Session 有 Creating Extensions for iOS and OS X, Part 1 Creating Extensions for iOS and OS X, Par ...

  4. iOS UIButton文字和图片间距随意调整

    代码地址如下:http://www.demodashi.com/demo/11606.html 前记 在开发中,我们经常会遇到这么一种情况,就是一个按钮上面有图片也有文字,但是往往设计并不是我们想要的 ...

  5. 【iOS系列】-xib封装使用

    [iOS系列]-xib封装使用 Xib文件可以用来描述某一块局部的UI界面 Xib文件的加载 修改xib文件的大小size(Freeform) 第一: NSArray *objs = [[NSBund ...

  6. iOS学习系列 - 扩展机制category与associative

    iOS学习系列 - 扩展机制category与associative category与associative作为objective-c的扩展机制的两个特性,category即类型,可以通过它来扩展方 ...

  7. IOS UIButton用法详解

    这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用.   //这里创建一个圆角矩形的按钮UIButton *button1 = [UIButton buttonWi ...

  8. iOS UIButton 图片文字上下垂直布局 解决方案

    实现如图所示效果: 这是一个UIButton,需要改变image和title相对位置. 解决如下: //设置文字偏移:向下偏移图片高度+向左偏移图片宽度 (偏移量是根据[图片]大小来的,这点是关键)b ...

  9. iOS开发拓展篇—封装音频文件播放工具类

    iOS开发拓展篇—封装音频文件播放工具类 一.简单说明 1.关于音乐播放的简单说明 (1)音乐播放用到一个叫做AVAudioPlayer的类 (2)AVAudioPlayer常用方法 加载音乐文件 - ...

  10. jeecg扩展封装tag的那些事

    版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 目前公司开发一直使用的是jeecg框架,简单好用,但有时候不如自己写的随心所欲.最近项目遇到一个需求, 想封装配置加解密,本来 ...

随机推荐

  1. Wireshark远程抓包

    1.被监控主机上,在安装目录下点击rpcapd.exe,运行server服务,并关闭防火墙 2.监控机器上,Capture Options,点击Manage Interfaces,弹出如下对话框 选择 ...

  2. LightOJ-1008-Fibsieve`s Fantabulous Birthday(推公式)

    链接: https://vjudge.net/problem/LightOJ-1008 题意: Fibsieve had a fantabulous (yes, it's an actual word ...

  3. oracle查询数据库连接数相关

    select username,count(username) from v$session where username is not null group by username;--查询各个用户 ...

  4. 通过 Ajax 调取后台接口将返回的 json 数据绑定在页面上

    第一步: 编写基础的 html 框架内容,并引入 jquery: <!doctype html> <html lang="en"> <head> ...

  5. PHP全栈学习笔记30

    变量:整型(int) 浮点(float).布尔(bool).字符串(string) 混合类型: 数组(array).对象(object) 特殊类型:空(null). 资源(resouce).回调(ca ...

  6. PHP sha1()函数

    <!DOCTYPE html> <html> <body> <?php $str = "dashu"; echo sha1($str); ...

  7. ROS中make_plan服务的使用

    路径规划:从一个点到另一个点,规划出最优的路线.用到service :make_plan (nav_msgs/GetPlan) 服务名为move_base_node/make_plan nav_msg ...

  8. Spring boot + mybatis 只读取到一个jar包中的mapper配置文件

    采用spring boot  开发了一个多模块项目,有多个模块中都有mapper配置文件. 采用如下的方式配置,制度去到了一个模块jar包中配置文件: @Bean(name = "sqlSe ...

  9. 学习OpenCV双目测距原理及常见问题解答

    学习OpenCV双目测距原理及常见问题解答 转自博客:https://blog.csdn.net/angle_cal/article/details/50800775 一. 整体思路和问题转化.  图 ...

  10. Beta冲刺(2/4)

    队名:福大帮 组长博客链接:https://www.cnblogs.com/mhq-mhq/p/11990570.html 作业博客 : https://edu.cnblogs.com/campus/ ...