在我的ViewController的ios中,我添加了四个按钮及其相关指示符OK
当我点击button4然后按钮4时,它的指示灯颜色必须为黑色,所有其他所有按钮及其指示灯颜色必须为白色。当我单击button2然后按钮2时,文本颜色和指示器必须为黑色。其余按钮文本颜色和指示符必须为白色,如下图所示
为此,我写了一些代码,但它没有用。
请你帮帮我
#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
答案 0 :(得分:1)
您可以将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...
答案 1 :(得分:0)
调用方法,不需要Buttontitle
,按钮获取所有控件的所有权
button = [[UIButton alloc] init];
//set the frame for your button
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitleColor:[UIColor GreenColor] forState:UIControlStateNormal];
[button setTitle:Title forState:UIControlStateNormal];
[button addTarget:self
action:@selector(ButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
确实是sender
,发件人是您当前的按钮Buttontitle
- (无效)ButtonAction:(的UIButton *)发件人{
if(![sender isSelected]){
for(UIView* view in sender.subviews){
if([view isKindOfClass:[UIView class]] ){
view.backgroundColor = [UIColor blueColor];
NSLog(@"the view name ==%@",view);
}
else{
}
}
[sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// StripView.backgroundColor = [UIColor blackColor];
sender.selected = YES;
}
else{
for(UIView* view in sender.subviews){
if([view isKindOfClass:[UIView class]] ){
view.backgroundColor = [UIColor yellowColor];
NSLog(@"the view name ==%@",view);
}
else{
}
}
[sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// StripView.backgroundColor = [UIColor clearColor];
sender.selected = NO;
}
}
答案 2 :(得分:0)
您可以在按钮操作方法中执行以下操作
<div class="slider" id="main-slider"></div>
答案 3 :(得分:0)
在方法&#34; createButton&#34;
中设置StripView的标记值StripView.tag = 10;
像这样更改ButtonAction方法
-(void)buttonAction:(UIButton*)sender{
for(UIView* view in TabBar.subviews){
UIButton *btn = (UIButton *)view;
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[[sender viewWithTag:10] setBackgroundColor:[UIColor clearColor]];
}
[sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[sender viewWithTag:10] setBackgroundColor:[UIColor blackColor]];
}
答案 4 :(得分:0)
我现在没有XCode,因此我通过成像键入代码。
NSArray *buttonCollection; ///< IBOutletCollection of UIButtons
- (void)buttonAction:(UIButton *)sender
{
// Make all button un-selected
[buttonCollection setValue:@(NO) forKeyPath:@"selected"];
// Make the sender UIButton selected
sender.selected = YES;
}
所选的UIButton文本颜色为黑色,未选中时,其文本颜色为白色。您可以按Jiri's answer
设置UIButton
州
选定的UIButton
似乎有自定义下划线。我更喜欢子类UIButton
,并在选择它时更改它的UI行为。