更改按钮的背景颜色

时间:2018-02-09 05:47:04

标签: ios objective-c iphone uibutton subclass

我正在开发一个项目,我在其中为UIButton创建一个子类来设置渐变背景颜色。它工作正常但是现在ViewController中有两个选项卡,有两个按钮,假设A和B.我想改变颜色按钮A当我点击按钮B时,与标签功能相同:

UIButton的子类:

// .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]];

  }

标签就像: I want to change these tab color alternate by taping on buttons

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

enter image description here

3 个答案:

答案 0 :(得分:0)

您应该为UIButton多状态设置状态。 例如,为不同的状态设置不同的背景图像。

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

然后,只控制状态。

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

答案 1 :(得分:0)

谢谢大家的回复。 我通过更改我的 - (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];
}

答案 2 :(得分:0)

只需转到故事板中的属性检查器,在底部看到它可以随之改变背景颜色。

见这里: -

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