一般來說我們在app中經常用到一些純色的響應按鈕,一般來說這些控件我們都會用UIButton來做,直接用
loginBtn.backgroundColor = UIColor.colorWithHex(hexRGBValue: 0xff7500)
就可以達到UI想要的效果,但是這樣確沒有了響應效果;但是UIButton的setBackgroundImage可以完美的解決這個問題,所以現在問題就集中在如何建造對應的純色圖片,廢話不說,直接上demo
OC制作純色圖片
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;
Swift制作純色圖片
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(color.cgColor)
context!.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!