iOS button点击点击状态下改变背景颜色
2020年02月13日
作者:佚名
下面是来客网 jb51.cc 通过网络收集整理的代码片段。
来客网小编现在分享给大家,也给大家做个参考。
//添加image分类 .h文件代码 #import "UIImage+Extension.h" @implementation UIImage (Extension) + (UIImage *)imageWithColor:(UIColor *)color { CGFloat imageW = 3; CGFloat imageH = 3; // 1.开启基于位图的图形上下文 UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageW,imageH),NO,0.0); // 2.画一个color颜色的矩形框 [color set]; UIRectFill(CGRectMake(0,imageW,imageH)); // 3.拿到图片 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // 4.关闭上下文 UIGraphicsEndImageContext(); return image; } @end //image分类 .m文件代码 #import <UIKit/UIKit.h> @interface UIImage (Extension) /** * 生成的图片的rect默认为100,100 */ + (UIImage *)imageWithColor:(UIColor *)color; @end //导入头文件 调用 分类方法 可改变image背景颜色 UIButton *loginButton = [[UIButton alloc]initWithFrame:self.view.bounds]; [loginButton setTitle:@"登 录" forState:UIControlStateNormal]; loginButton.backgroundColor = [UIColor whiteColor]; [loginButton setBackgroundImage:[UIImage imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateHighlighted]; [self.view addSubview:loginButton];
以上是来客网(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。
以上是来客网为你收集整理的iOS button点击点击状态下改变背景颜色全部内容,希望文章能够帮你解决iOS button点击点击状态下改变背景颜色所遇到的程序开发问题。
如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。