更改按钮的背景颜色

Change button's background colour

我正在做一个项目,我在其中为 UIButton 创建了一个子 class 来设置渐变背景 colour.Its 工作正常,但现在 ViewController 中有两个选项卡假设有两个按钮 A 和 B。我想在单击按钮 B 时更改按钮 A 的颜色,就像选项卡功能一样:

UIButton 的子 class:

// .h_file
#import <UIKit/UIKit.h>

IB_DESIGNABLE
@interface grediantButton : UIButton

@property(nonatomic)IBInspectable UIColor*topColor;
@property(nonatomic)IBInspectable UIColor*bottomColor;

@property(nonatomic)IBInspectable CGFloat cornerRadius;

- (void)customInit;
@end

// .m file
#import "grediantButton.h"
#import "UIColor+myColor.h"
@implementation grediantButton

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {

        self.topColor = [UIColor clearColor];
        self.bottomColor = [UIColor clearColor];
        self.cornerRadius = 1;
        [self customInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self customInit];
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    [self customInit];
}

- (void)setNeedsLayout {
    [super setNeedsLayout];
    [self setNeedsDisplay];
}


- (void)prepareForInterfaceBuilder {

    [self customInit];
}

- (void)customInit {

    self.layer.cornerRadius = self.cornerRadius;

    CAGradientLayer *gradientLayer =  [[CAGradientLayer alloc] init];
    gradientLayer.frame = self.bounds;


    gradientLayer.cornerRadius = self.cornerRadius;

    gradientLayer.colors = [NSArray arrayWithObjects:(id)[self.topColor CGColor], (id)[self.bottomColor CGColor],nil];

    [self.layer setMasksToBounds:YES];
    [self.layer insertSublayer:gradientLayer atIndex:0];

}

我的VC代码:

#pragma mark:AddnewTownshipVC
- (IBAction)addnewTownShip:(id)sender {

    [self.addnewTwnBtn setTopColor:[UIColor getTabTopColor]];
    [self.addnewTwnBtn setBottomColor:[UIColor getTabBotColor]];


    [self.viewalltownBtn setTopColor:[UIColor blackColor]];
    [self.viewalltownBtn setBottomColor:[UIColor blackColor]];

}

#pragma mark:viewalltownshipVC
- (IBAction)viewAllTownship:(id)sender {

    [self.addnewTwnBtn setTopColor:[UIColor blackColor]];
    [self.addnewTwnBtn setBottomColor:[UIColor blackColor]];

    [self.viewalltownBtn setTopColor:[UIColor getTabTopColor]];
    [self.viewalltownBtn setBottomColor:[UIColor getTabBotColor]];

  }

标签就像:

当我点击黑色的一个标签时会发生什么

您应该为 UIButton 设置多个状态。 比如不同的状态设置不同的背景图。

[button setBackgroundImage:naomalImage forState:UIControlStateNormal];
[button setBackgroundImage:selectedImage forState:UIControlStateSelected];

然后,就是控制状态。

- (IBAction)addnewTownShip:(id)sender {
    button.selected = YES;
}
...

谢谢大家的回复。 我通过更改我的 - (void)customInit 方法

中的一些代码解决了我的问题
- (void)customInit {


    self.layer.cornerRadius = self.cornerRadius;

    CAGradientLayer *gradientLayer =  [[CAGradientLayer alloc] init];
    gradientLayer.frame = self.bounds;


    gradientLayer.cornerRadius = self.cornerRadius;

    gradientLayer.colors = [NSArray arrayWithObjects:(id)[self.topColor CGColor], (id)[self.bottomColor CGColor],nil];

    [self.layer setMasksToBounds:YES];

    if([[self.layer sublayers] objectAtIndex:0]){
        [self.layer replaceSublayer:[[self.layer sublayers] objectAtIndex:0] with:gradientLayer];
    }else{
            [self.layer insertSublayer:gradientLayer atIndex:0];
    }

}

并在

中调用此方法
#pragma mark:AddnewTownshipVC
- (IBAction)addnewTownShip:(grediantButton*)sender {

    [sender setTopColor:[UIColor getTabTopColor]];
    [sender setBottomColor:[UIColor getTabBotColor]];

    [sender customInit];


    [self.viewalltownBtn setTopColor:[UIColor blackColor]];
    [self.viewalltownBtn setBottomColor:[UIColor blackColor]];

    [self.viewalltownBtn customInit];

//    sender.isSelected = true;
//    self.viewalltownBtn.isSelected = false;

}

#pragma mark:viewalltownshipVC
- (IBAction)viewAllTownship:(grediantButton*)sender {

    [self.addnewTwnBtn setTopColor:[UIColor blackColor]];
    [self.addnewTwnBtn setBottomColor:[UIColor blackColor]];

    [self.addnewTwnBtn customInit];

    [sender setTopColor:[UIColor getTabTopColor]];
    [sender setBottomColor:[UIColor getTabBotColor]];
    [sender customInit];
}

只需转到情节提要中的属性检查器,在底部看到它有背景颜色,您可以使用它进行更改。

看这里 :-

https://i.stack.imgur.com/A6ecY.png