Reputation:
In ios in my ViewController I have added four buttons and their related indicators OK
When I tap on button4 then button4 and it's indicator color must be black color and all the other all buttons and their indicators colors must be white. When I click button2 then button2 the textcolor and indicator must be black. The remaining buttons textcolors and indicators must be white as like below image
For this I have written some code but it's not working.
Please can you help me
#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
Upvotes: 0
Views: 101
Reputation: 82759
call the method like , no need of Buttontitle
, button takes the ownership of all controls
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];
do like it is an sender
, sender as your current button not Buttontitle
-(void)ButtonAction:(UIButton*)sender{
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;
}
}
Upvotes: 0
Reputation: 18378
I have no XCode now, so I type codes by imaging.
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;
}
The selected UIButton text color is black, and when they are un-selected, their text color is white. You can set UIButton
state by Jiri's answer
It seems like the selected UIButton
has a custom underline. I will prefer to subclass UIButton
, and change it's UI behavior when it is selected.
Upvotes: 0
Reputation: 2217
Set the tag value for StripView in method "createButton"
StripView.tag = 10;
Change the ButtonAction method like this
-(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]];
}
Upvotes: 0
Reputation: 506
You can do something like below in your button action method
-(void)buttonAction:(UIButton*)sender{
for(UIView* view in self.view.subviews){
if([view isKindOfClass:[UIButton class]] && view != sender){
UIButton *btn = (UIButton *)view;
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // set color whichever you want
}
else{
[sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // set color which ever you want
}
}
}
Upvotes: 0
Reputation: 597
you can define UIButton as follow code:
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...
Upvotes: 1