在我的 ViewController 中,我添加了四个按钮及其相关的指示符 OK
当我点击 button4 然后点击 button4 时,它的指示器颜色必须为黑色,所有其他所有按钮及其指示器颜色必须为白色。当我单击 button2 然后单击 button2 时,文本颜色和指示符必须为黑色。其余按钮的文本颜色和指示器必须为白色,如下图所示
为此,我编写了一些代码,但它不起作用。
你能帮帮我吗
我的代码:-
#import "ViewController12.h"
#import "Masonry.h"
@interface ViewController12 ()
{
UIView * TabBar;
UIButton * AllTrips;
UIButton * OpenTrips;
UIButton * AssignedTrips;
UIButton * CompletedTrips;
UIButton * button;
UIView * StripView;
UILabel * label;
}
@end
@implementation ViewController12
- (void)viewDidLoad {
TabBar = [[UIView alloc]init];
TabBar.backgroundColor = [UIColor lightGrayColor];
TabBar.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:TabBar];
[TabBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@0);
make.left.equalTo(@0);
make.right.equalTo(@0);
make.height.equalTo(@100);
}];
label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor clearColor];
label.text = @"Hello";
label.backgroundColor = [UIColor clearColor];
label.translatesAutoresizingMaskIntoConstraints = NO;
[TabBar addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@0);
make.left.equalTo(@10);
make.right.equalTo(@-10);
make.height.equalTo(@10);
make.centerX.equalTo(TabBar);
}];
AllTrips = [self createButton:@"All"];
AllTrips.tag = 1;
[TabBar addSubview: AllTrips];
OpenTrips = [self createButton:@"Open"];
OpenTrips.tag = 2;
[TabBar addSubview: OpenTrips];
AssignedTrips = [self createButton:@"Assigned"];
AssignedTrips.tag = 3;
[TabBar addSubview: AssignedTrips];
CompletedTrips = [self createButton:@"Completed"];
CompletedTrips.tag = 4;
[TabBar addSubview: CompletedTrips];
NSDictionary * views = NSDictionaryOfVariableBindings(AllTrips,OpenTrips,AssignedTrips,CompletedTrips);
[TabBar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-5-[AllTrips]-2-[OpenTrips]-2-[AssignedTrips]-2-[CompletedTrips]-5-|"]
options:0
metrics:nil
views:views]];
[TabBar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[AllTrips(==OpenTrips)]" options:0 metrics:nil views:views]];
[TabBar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[OpenTrips(==AssignedTrips)]" options:0 metrics:nil views:views]];
[TabBar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[AssignedTrips(==CompletedTrips)]" options:0 metrics:nil views:views]];
NSArray * keys = @[@"AllTrips",@"OpenTrips",@"AssignedTrips",@"CompletedTrips"];
for (NSString * key in keys) {
[TabBar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-50-[%@(50)]",key]
options:0
metrics:nil
views:views]];
}
}
-(UIButton *)createButton:(NSString*)Title{
button = [[UIButton alloc] init];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTitle:Title forState:UIControlStateNormal];
[button addTarget:self
action:@selector(ButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor orangeColor];
StripView = [[UIView alloc]init];
StripView.translatesAutoresizingMaskIntoConstraints = NO;
StripView.backgroundColor = [UIColor clearColor];
[button addSubview:StripView];
NSDictionary * StripDic = NSDictionaryOfVariableBindings(StripView);
[button addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[StripView]-0-|"]
options:0
metrics:nil
views:StripDic]];
[button addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[StripView(5)]-0-|"]
options:0
metrics:nil
views:StripDic]];
return button;
}
-(void)ButtonAction:(UIButton*)sender{
if(![sender isSelected]){
[sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
StripView.backgroundColor = [UIColor blackColor];
sender.selected = YES;
}
else{
[sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
StripView.backgroundColor = [UIColor clearColor];
sender.selected = NO;
}
}
@end
最佳答案
您可以按以下代码定义 UIButton:
UIButton *btn = [UIButton new];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
//etc...
关于ios - 当我们在 ios 中点击它时如何更改 UIbuttons 文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33671355/