user1676682
user1676682

Reputation: 381

Highlighting my buttons in iOS 7

I'm making a custom keypad in iOS 7, and I want the buttons' background color to change upon a user's tap. However, I can't get the button to do so. In the IBAction that I'm assigning to the buttons, the code I'm using is

    if(button.highlighted==YES){
    button.backgroundColor = [UIColor blackColor]; //Change background color
    button.titleLabel.textColor = [UIColor whiteColor]; //Change text color
    }

What am I doing wrong here? Am I misusing "highlighted"? This code doesn't seem to affect the user interface at all when I run it and I'm not sure what to replace it with.

Upvotes: 2

Views: 4352

Answers (4)

CularBytes
CularBytes

Reputation: 10321

In Swift, you can do this:

extension UIButton {
func setBackgroundColor(color: UIColor, forState: UIControlState) {
    UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), color.CGColor)
    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRect(x: 0, y: 0, width: 1, height: 1))
    let colorImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    self.setBackgroundImage(colorImage, forState: forState)
}}

Just have that written outside any class and you can use it everywhere like so:

yourButton.setBackgroundColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)

Source

Upvotes: 0

Pradhyuman sinh
Pradhyuman sinh

Reputation: 3928

Change the highlighted state of the button:

[YourButton setBackgroundImage:[self imageWithColor:[UIColor greenColor]] forState:UIControlStateHighlighted];

And add this method to your view controller:

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

Upvotes: 4

Hussain Shabbir
Hussain Shabbir

Reputation: 15035

Well simple thing you can do in your xib also. Follow the below screens shot:-

1) Select the button and then inside drawing and control section enable show Touch on Highlight and Highlighted enter image description here

Upvotes: 1

tassar
tassar

Reputation: 588

+ (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;
}

you create a image with this method, and setup your button with setBackgroundImage:forState: method

Upvotes: 0

Related Questions