更改按钮的背景颜色

Posted

技术标签:

【中文标题】更改按钮的背景颜色【英文标题】:Change button's background colour 【发布时间】:2018-02-09 05:47:04 【问题描述】:

我正在做一个项目,在该项目中,我为 UIButton 创建了一个子类,用于设置渐变背景颜色。它工作正常,但现在 ViewController 中有两个选项卡,假设 A 和 B 有两个按钮。我想更改颜色当我单击按钮 B 时,按钮 A 与选项卡功能相同:

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

  

标签就像:

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

【问题讨论】:

那是什么问题? 我无法更改代码“[self.addnewTwnBtn setTopColor:[UIColor blackColor]]; [self.addnewTwnBtn setBottomColor:[UIColor blackColor]];”中显示的颜色 很明显,只需将值传递给属性bottomColor,它将如何设置颜色?你没有为此编写任何代码 为按钮选择状态设置金色和正常状态设置黑色做一件事。现在点击按钮只需更改sender.selected = !sender.selected 每次单击时都会调用此方法 - (void)customInit。我把所有的想法都放在里面。你可以查看上面的代码 【参考方案1】:

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

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

然后,只是控制状态。

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

...

【讨论】:

【参考方案2】:

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

【讨论】:

【参考方案3】:

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

看这里:-

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

【讨论】:

以上是关于更改按钮的背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

用我选择的颜色更改按钮背景颜色[重复]

按下时更改操作栏按钮的背景颜色

更改按钮的背景颜色

使用 swift 语言更改按钮背景颜色

可以更改容器背景颜色/颤动颜色的按钮

更改圆形按钮android的背景颜色