iOS UIButton setting the background color in the highlighted state

Source: Internet
Author: User

One, set the background color by the button's event

12345678910111213141516171819202122 - (void)viewDidLoad {    [superviewDidLoad];        UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 50)];    [button1 setTitle:@"button1"forState:UIControlStateNormal];    button1.backgroundColor = [UIColor orangeColor];    [button1 addTarget:selfaction:@selector(button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown];    [button1 addTarget:selfaction:@selector(button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button1];}//  button1普通状态下的背景色- (void)button1BackGroundNormal:(UIButton *)sender{    sender.backgroundColor = [UIColor orangeColor];}//  button1高亮状态下的背景色- (void)button1BackGroundHighlighted:(UIButton *)sender{    sender.backgroundColor = [UIColor greenColor];}

Second, by converting the color to uiimage as the background image of the button in different states

123456789101112131415161718192021222324 - (void)viewDidLoad {    [superviewDidLoad];        UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(170, 200, 100, 50)];    [button2 setTitle:@"button2" forState:UIControlStateNormal];    [button2 setBackgroundImage:[selfimageWithColor:[UIColor redColor]] forState:UIControlStateNormal];    [button2 setBackgroundImage:[selfimageWithColor:[UIColor grayColor]] forState:UIControlStateHighlighted];    [self.view addSubview:button2];}//  颜色转换为背景图片- (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();        returnimage;}

Three

1. First, add a button on the interface, we first set the normal and highlight the text when the state, there are rounded corners

    uibutton *button = [[uibutton Alloc] Initwithframe:cgrectmake (100, 100, 100, 40)]; [Button Settitle:@ "normal" Forstate:uicontrolstatenormal]; [Button Settitle:@ "highlighted" Forstate: Uicontrolstatehighlighted]; [Button Setbackgroundcolor:[uicolor Redcolor]]; Button.layer.cornerRadius = 10.0f; Button.layer.masksToBounds = yes; [self.view Addsubview:button]; //add Observer method [self Addobserver:button];     

2. Add the Viewer

/** *  添加观察者 * *  @param button 需要设置的按钮 */- (void)addObserver:(UIButton *)button {        [button addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew context:nil];}

3. The next step is to implement the observer approach

-(void) ObserveValueForKeyPath :(nsstring *) KeyPath ofobject: (id) object change: (nsdictionary<nsstring *,id> * Change context: (void *) Context {uibutton *button = (uibutton *) object; if ([KeyPath isequaltostring:@ "highlighted"]) {if (button.highlighted) {[Button Setbackgroundcolor:[UIColor BlueColor]] ; return;} [Button Setbackgroundcolor:[uicolor Redcolor]];}       

This allows us to set the background color of the highlighted and normal state without using the image.
Look at the concrete effect.

iOS UIButton setting the background color in the highlighted state

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.