Reputation: 3841
When a UIButton
gets selected, how can I get it to select its full frame? Here is what I mean:
So the orange is the UIButtons
frame, and the blue is when it's selected. How can I get the blue to fill the whole frame? (So you wouldn't see the orange.)
Update
Upvotes: 2
Views: 369
Reputation: 2431
I'm assuming that the orange is the superview of the UIButton
and that the superview is what you want to have the tint color applied to. If that's the case, then I would add a target to the button to change the superview's backgroundColor
to match the tintColor
of the button. Something like this should work:
[myButton addTarget:self
action:@selector(setButtonSuperviewToTintColor:)
forControlEvents:UIControlEventTouchUpInside];
and then elsewhere in the controller add:
- (void)setButtonSuperviewToTintColor:(UIButton *)sender {
sender.superview.backgroundColor = sender.tintColor;
}
If the superview is not what you want to adjust, then create a method that changes the UIButton
's background color to it's tint color.
[myButton addTarget:self
action:@selector(setButtonBackgroundColorToTintColor:)
forControlEvents:UIControlEventTouchUpInside];
and elsewhere:
- (void)setButtonBackgroundColorToTintColor:(UIButton *)sender {
sender.backgroundColor = sender.tintColor;
}
However, if you just want this to be the selected
color, then there's kind of a method for this already. Unfortunately, it's designed to work with an image. But, making a color from an image is easy. So, this should work for you:
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
[[UIColor blueColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[myButton setBackgroundImage:img forState:UIControlStateSelected];
Upvotes: 2