UIButton not showing highlight when touched

You can create an image to fill in the background with color.

[cancelButton setBackgroundImage:[self setBackgroundImageByColor:[UIColor blueColor] withFrame:cancelButton.frame cornerRadius:0] forState:UIControlStateHighlighted];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

This method builds an image the size of the button with a solid background of the color backgroundColor.

-(UIImage *)setBackgroundImageByColor:(UIColor *)backgroundColor withFrame:(CGRect )rect cornerRadius:(float)radius{

    UIView *tcv = [[UIView alloc] initWithFrame:rect];
    [tcv setBackgroundColor:backgroundColor];

    CGSize gcSize = tcv.frame.size;
    UIGraphicsBeginImageContext(gcSize);
    [tcv.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    const CGRect RECT = CGRectMake(0, 0, image.size.width, image.size.height);;
    [[UIBezierPath bezierPathWithRoundedRect:RECT cornerRadius:radius] addClip];
    [image drawInRect:RECT];
    UIImage* imageNew = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageNew;
}

I know you wanted a white highlight color, but for anyone else that doesn't care specifically what color the highlight is, just switch the button type to System (in the Attribute Inspector). The nice thing is that you can still use custom fonts, colors, etc.


To get the white text on highlight just do:

[cancelButton setTitleColor:[UIColor whiteColor]
                       forState:UIControlStateHighlighted];

To get the background to change you need to either do setBackgroundImage:forState: and use a UIImage with the pattern color, or subclass UIButton and set the appropriate background color in the setHighlight: method.

EDIT: Swift 2.x version

cancelButton.setTitleColor(.whiteColor(), forState: Highlighted)

EDIT: Swift 3.0 version

cancelButton.setTitleColor(.white, for: .highlighted)