Objective-C highlight UIButton if selected / not selected

I have a UIButton inside a UIView, when UIButton is selected, I would like to have a dark gray background color ... is this possible?

UIView *toolbar = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 40, 160)];
    [toolbar setBackgroundColor:[UIColor colorWithWhite: 0.70 alpha:0.5]];

    toolbar.layer.cornerRadius = 5;

    UIButton *penTool = [UIButton buttonWithType:UIButtonTypeCustom];
    penTool.frame = CGRectMake(5, 0, 30, 30);
    [penTool setImage:[UIImage imageNamed:@"pen-but" inBundle:currentBundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
    [penTool addTarget:self action:@selector(drawButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    penTool.autoresizingMask = UIViewAutoresizingNone;
    penTool.exclusiveTouch = YES;
    penTool.tag = 1;

    [toolbar addSubview:penTool];
+4
source share
9 answers

What if you can do it?

[button setBackgroundColor:[UIColor greenColor] 
                  forState:UIControlStateSelected];

Let me give you an excellent API for each state, not just the one selected, as well as the default UIButton API for changing the image and name for each state.

BGButton.h

#import <UIKit/UIKit.h>

@interface BGButton : UIButton

- (void)setBackgroundColor:(UIColor *)backgroundColor 
                  forState:(UIControlState)state;

@end

BGButton.m

#import "BGButton.h"

@implementation BGButton {
    NSMutableDictionary *_stateBackgroundColor;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _stateBackgroundColor = [[NSMutableDictionary alloc] init];
    }
    return self;
}


- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:backgroundColor];

   /*
    * If user set button.backgroundColor we will set it to 
    * normal state backgroundColor.
    * Check if state is on normal state to prevent background being 
    * changed for incorrect state when updateBackgroundColor method is called.
    */
    if (self.state == UIControlStateNormal) {
        [self setBackgroundColor:backgroundColor forState:UIControlStateNormal];
    }
}

- (void)setBackgroundColor:(UIColor *)backgroundColor 
                  forState:(UIControlState)state {
    NSNumber *key = [NSNumber numberWithInt:state];
    [_stateBackgroundColor setObject:backgroundColor forKey:key];
}

/*
 * state is synthesized from other flags. So we can't use KVO 
 * to listen for state changes.
 */
- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    [self stateDidChange];
}

- (void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    [self stateDidChange];
}

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    [self stateDidChange];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self stateDidChange];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    [self stateDidChange];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self stateDidChange];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    [self stateDidChange];
}

- (void)stateDidChange {
    [self updateBackgroundColor];
}

- (void)updateBackgroundColor {
    NSNumber *key = [NSNumber numberWithInt:self.state];
    self.backgroundColor = [_stateBackgroundColor objectForKey:key];
}

@end


You can use it as default UIButton API

BGButton *button = [[BGButton alloc] init];

[button setBackgroundColor:[UIColor greenColor] 
                  forState:UIControlStateSelected];

[button setBackgroundColor:[UIColor blueColor] 
                  forState:UIControlStateHighlighted];

[button setBackgroundColor:[UIColor orangeColor] 
                  forState:UIControlStateDisabled];

Even if you use

button.backgroundColor = [UIColor redColor];

You will remain safe, it will be translated into

[button setBackgroundColor:[UIColor redColor] 
                  forState:UIControlStateNormal];
+7
source

if you have this code

[self.view addSubview:toolbar];

then just do the selector

-(void)drawButtonTapped:(UIButton*)button{

if (button.selected) {
    button.backgroundColor = [UIColor clearColor];
    button.selected = NO;
}else{
    button.backgroundColor = [UIColor darkGrayColor];
    button.selected = YES;
}
}
+2

, , , UIButton. , , UIButtonTypeSystem .

[UIButton buttonWithType:UIButtonTypeSystem]
, . .

-

STORYBOARD

SIMULATOR

, tintColor .

  • . .
  • , , , .

, iOS7 . UIButtonTypeSystem iOS7 3 .

!

+2

UIButton :

#import "CustomButton.h"

@interface CustomButton()
@property (nonatomic,strong) UIColor *originalColor;
@end

@implementation CustomButton

- (void)didMoveToSuperview
{
    self.originalColor = self.superview.backgroundColor;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.superview.backgroundColor = [UIColor grayColor];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    if ([self hitTest:location withEvent:nil]) {
        self.superview.backgroundColor = [UIColor grayColor];
        self.originalColor = self.superview.backgroundColor;
    }
    else
    {
        self.superview.backgroundColor = self.originalColor;
    }
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.superview.backgroundColor = self.originalColor;    
}

@end

, . . , , touchhesBegan.

+2

UIButton ...

   - (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];

    if (highlighted) {
        self.backgroundColor = [UIColor darkGray];
    }

}
0

, , .

(, pen-but-selected). setImage:

[penTool setImage:[UIImage imageNamed:@"pen-but-selected" inBundle:currentBundle compatibleWithTraitCollection:nil] forState:UIControlStateSelected];

- (void)drawButtonTapped:(UIButton *)button {
    button.selected = !button.selected;
}

. @jamil . , buttonImage , buttonBackground buttonImage.

0

Try using a logical or some other reference to the button selection state. You can also use "highlightColour" as well as "selectedColour" if you need more careful control (perhaps this is for a better UX).

bool buttonIsSelected = false;

UIColor * selectedColour = [UIColor redColor];
UIColor * defaultColour = [UIColor blueColor];

UIButton * button = [UIButton new];
button.frame = CGRectMake(0,0,100,100);
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];

[button addTarget:self action:@selector(buttonTouchUp:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(buttonTouchCancel:) forControlEvents:UIControlEventTouchCancel];
[button addTarget:self action:@selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];

-(void)buttonTouchUp:(UIButton *)button {

    if (buttonIsSelected){
        buttonIsSelected = false;
        button.backgroundColor = defaultColour;
    } else {
        buttonIsSelected = true;
        button.backgroundColor = selectedColour;
    }
}
-(void)buttonTouchCancel:(UIButton *)button {
    button.backgroundColor = defaultColour;        
    if (buttonIsSelected){
        button.backgroundColor = selectedColour;
    }
}
-(void)buttonTouchDown:(UIButton *)button {
    button.backgroundColor = selectedColour;
}
0
source
-(void)OnSelect:(UIButton*)button{

if (button.selected) {
    button.backgroundColor = [UIColor blue];
    button.selected = NO;
}else{
    button.backgroundColor = [UIColor darkGrayColor];
    button.selected = YES;
}
}

use the above, it will work.

0
source

- (Invalid) buttonTapped: (UIButton *) Button {

button.selected =! button.selected;

if (button.selected)
    button.backgroundColor = [UIColor grayColor];

else
    button.backgroundColor = [UIColor clearColor];

}

-1
source

All Articles