1

Since the UISegmentedControl cannot be customized in IOS 7 , and since my project deserves a certain degree of customization, i decided to create two UIButtons Manually, with a bool variable to tell which button is clicked, and the reload a tableView :

- (IBAction)pastFun:(id)sender {
    statusClicked = FALSE;
    NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"PAST"];

    [commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];

    [pastBut setAttributedTitle:commentString forState:UIControlStateNormal];
    [self.tableView reloadData];
}
- (IBAction)pendingFun:(id)sender {

    statusClicked = TRUE;
    [self.tableView reloadData];
}

in pastFun, i added the following block in order to underline the button when selected, and it works :

NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"PAST"];

[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];

[pastBut setAttributedTitle:commentString forState:UIControlStateNormal];

What i want to do is, when the pendingFun is called ( next button is clicked), remove the underline from the first button and then underline the second, i can't seem to find the right way to accomplish that. Any ideas how to do so?

rmaddy
  • 298,130
  • 40
  • 468
  • 517
Elias Rahme
  • 2,236
  • 3
  • 24
  • 51
  • 1
    The best way is create one images that background is transparent with single line and set this image as background of your selected button. – iPatel Apr 30 '14 at 09:53
  • Is your issue about removing the underline attribute? Check that: http://stackoverflow.com/questions/23246543/how-to-remove-underline-in-uibutton-in-ios7-1-programetically/23246993#23246993 – Larme Apr 30 '14 at 09:55
  • Try...in link [How to Display Underlined Text in a Button?](http://stackoverflow.com/a/35066755/1947330) – Ar No Sep 10 '16 at 15:00

1 Answers1

5

I think it's best to select and deselect the button.

- (void)viewDidLoad
{
    [super viewDidLoad];        

    NSAttributedString *attrNormal = [[NSAttributedString alloc] initWithString:@"Button" attributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleNone]}];
    NSAttributedString *attrSelected = [[NSAttributedString alloc] initWithString:@"Button" attributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]}];

    UIButton* boton = [[UIButton alloc] initWithFrame:CGRectMake(20, 50, 100, 30)];
    [boton setAttributedTitle:attrNormal forState:UIControlStateNormal];
    [boton setAttributedTitle:attrSelected forState:UIControlStateSelected];
    [boton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:boton];
}

- (void) buttonPressed:(UIButton *) boton {
    boton.selected = !boton.selected;
}

In your case, with two buttons. You'll just have to keep a property to store the selected button (self.selectedButton for example). And in buttonPressed do this:

- (void) buttonPressed:(UIButton *) boton {
  self.selectedButton.selected = NO;
  self.selectedButton = boton;
  self.selectedButton.selected = YES;
}
Odrakir
  • 4,206
  • 1
  • 18
  • 51