Search code examples
iosobjective-cuibuttonuicontrolstate

Custom button not changing text color when selected


I have created 3 custom buttons using for loop. But when I select a button the text color is not changing.How do I do it? What else I have to add?

Here is my Code

buttonText = [[NSArray alloc]initWithObjects: @"Slambook",@"Initiated",@"Collaborated",nil];    
NSInteger numControllers = [viewControllerArray count];

for (int i = 0; i<numControllers; i++) {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
    [button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateSelected];
    [button setTitle:[buttonText objectAtIndex:i] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [navigationView addSubview:button];
}

Solution

  • I have tried your code for single button with dummy text , black text colour for normal state and button colour is highlighting on tap:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake(20, 40,50 , 35);
    //        button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
        [button setTitle:@"Dummy" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];
    
            [button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:button];
    

    Below line does not change button text colour for long time, it change text colour of button for fraction of seconds after tap

    [button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];

    Try this:

     NSInteger numControllers = [viewControllerArray count];
    
        for (int i = 0; i<numControllers; i++) {
    
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER,(self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
       button.tag=i+200;
            [button setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1]];
            [button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [navigationView addSubview:button];
        }
    - (IBAction)tapButtonAction:(id)sender
    {
    
        UIButton *btn = (UIButton*)sender;
    
            [btn setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
    
       for (int i = 0; i<numControllers; i++) {
           UIButton *restBtn=[navigationView viewWithTag:i+200];
        if(restbtn!= btn)
        {
             [restBtn setBackgroundColor:[UIColor grayColor] forState:   UIControlStateNormal];
            [restBtn setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
    }
    
        }
    }