0

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?

enter image description here

 -(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:

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70

2 Answers2

1

I've notice that you are using setAttributedTitle:forState:. You can set attributes for title in the same way but for UIControlStateSelected also.

Then, if you set button.selected = YES; attributes from UIControlStateSelected will apply. If you set button.selected = NO; attributes from UIControlStateNormal will apply.

EDIT:

You can create your buttons like this:

NSInteger numberOfButtons = 10;
NSMutableArray *menuButtonsMutableArray = [[NSMutableArray alloc] initWithCapacity:numberOfButtons];
for (int i = 0; i < numberOfButtons; i++) {
    UIButton *button = [UIButton new];
    //layout your button somehow
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(menuButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];

    [menuButtonsMutableArray addObject:button];
}

self.menuButtonsArray = [menuButtonsMutableArray copy];

Then in action method:

- (void)menuButtonDidTap:(UIButton *)sender {
    for (UIButton *button in self.menuButtonsArray) {
        button.selected = (button == sender);
    }
}
psci
  • 903
  • 9
  • 18
  • 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?I have created say 5 buttons.How should i apply your logic?I am not getting it – abhimuralidharan Oct 13 '15 at 06:46
  • @abhi1992 First of all, I would think about subclassing `UISegmentedControl`. This control seems to fit you needs quiet well. If you want to stay with your solution, you have to store all menu buttons in NSArray, then if user tap any of this button you'll have to iterate thru array and set state for all buttons. – psci Oct 13 '15 at 06:53
0
@IBOutlet var AllButtons: [UIButton]!
for button in AllButtons {
    if button.backgroundColor == UIColor.red {
        button.backgroundColor = compare.backgroundColor
    }
}

Drag and drop all your buttons in AllButtons, then use for loop to access all the buttons and do whatever you like to do.
This is a simple example, hope it helps.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Habibur Rahman
  • 194
  • 2
  • 8