In my app I have created a few buttons programmatically using a for loop as shown below.It is for a HORIZONTAL TAB MENU
In the action,I have to highlight the selected button(and greyout the remaining button titles).How to do this? It should look almost like the image below.
When a button is clicked,the clicked button title color should be white and all other buttons should have a grey color.I know I can access sender.titlecolor in the button action.But What about the other buttons?
 -(void)createButtons
 {
     float buttonMinX=10;
     float  buttonMinY=0;
     scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _tabViewBackground.frame.size.width, _tabViewBackground.frame.size.height)];
     ButtonIndex=0;
     scrollView.showsHorizontalScrollIndicator = NO;
     [_tabViewBackground addSubview:scrollView];
     for (int i=0; i<_tabItemsListArray.count; i++)
     {
         UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
         button.tintColor=[UIColor whiteColor];
         button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
         button.titleLabel.numberOfLines = 1;
         button.tag=i;
         [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
         UIFont *font1 = [UIFont fontWithName:@"HelveticaNeue-Thin" size:20.0f];
         NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
         [style setAlignment:NSTextAlignmentCenter];
         NSDictionary *dict1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),
                                    NSFontAttributeName:font1,
                                    NSParagraphStyleAttributeName:style};
         NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
         [attString appendAttributedString:[[NSAttributedString alloc] initWithString:[_tabItemsListArray objectAtIndex:i]  attributes:dict1]];
         [button setAttributedTitle:attString forState:UIControlStateNormal];
         buttonWidth= [self getWidthOfRect:button.titleLabel];
         button.frame = CGRectMake(buttonMinX,buttonMinY,buttonWidth,_tabViewBackground.frame.size.height);
         buttonMinX+=buttonWidth+05;
         sublayer = [[UIView alloc]init];
         sublayer.backgroundColor = [UIColor greenColor];
         sublayer.tag=kButtonSelectrorTag+i;
         sublayer.frame = CGRectMake(button.frame.origin.x,button.frame.size.height-2, button.frame.size.width,2);
         [scrollView addSubview:sublayer];
         sublayer.hidden=YES;
         if (ButtonIndex==i) 
         {
             sublayer.hidden=NO;
         }
         button.backgroundColor=[UIColor clearColor];
         [scrollView addSubview:button];
    }
    scrollView.backgroundColor = [UIColor blueColor];
    scrollView.contentSize = CGSizeMake(buttonMinX+10,_tabViewBackground.frame.size.height);
}
-(CGFloat)getWidthOfRect:(UILabel*)titleLabel
{
    CGFloat widthIs =[titleLabel.text boundingRectWithSize:titleLabel.frame.size options:NSStringDrawingUsesDeviceMetrics attributes:@{ NSFontAttributeName:titleLabel.font }context:nil].size.width;
        widthIs = ceilf(widthIs);
    //  NSLog(@"the width of yourLabel is %f", widthIs);
    return widthIs+30;
}
- (void)action:(UIButton*)sender
{
    for (int i=0; i<_tabItemsListArray.count; i++) 
    {
        UIView *tabSelector = (UIView *)[self.view viewWithTag:kButtonSelectrorTag+i];
        [tabSelector setHidden:YES];
    }
    UIView *tabSelector = (UIView *)[self.view viewWithTag:kButtonSelectrorTag+sender.tag];
    [tabSelector setHidden:NO];
}
I have a button selector below every button.I should show one buttonSelector at a time.It is working great using the code in the action:

 
     
     
     
    