UPDATE:
Use the UIButtonBackgroundColor Swift library.
OLD:
Use the helpers below to create a 1 px x 1 px image with a grayscale fill color:
UIImage *image = ACUTilingImageGray(248/255.0, 1);
or an RGB fill color:
UIImage *image = ACUTilingImageRGB(253/255.0, 123/255.0, 43/255.0, 1);
Then, use that image
to set the button's background image:
[button setBackgroundImage:image forState:UIControlStateNormal];
Helpers
#pragma mark - Helpers
UIImage *ACUTilingImageGray(CGFloat gray, CGFloat alpha)
{
return ACUTilingImage(alpha, ^(CGContextRef context) {
CGContextSetGrayFillColor(context, gray, alpha);
});
}
UIImage *ACUTilingImageRGB(CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha)
{
return ACUTilingImage(alpha, ^(CGContextRef context) {
CGContextSetRGBFillColor(context, red, green, blue, alpha);
});
}
UIImage *ACUTilingImage(CGFloat alpha, void (^setFillColor)(CGContextRef context))
{
CGRect rect = CGRectMake(0, 0, 0.5, 0.5);
UIGraphicsBeginImageContextWithOptions(rect.size, alpha == 1, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
setFillColor(context);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Note: ACU
is the class prefix of my Cocoa Touch Static Library called Acani Utilities, where AC is for Acani, and U is for Utilities.