shebi
shebi

Reputation: 719

Change UISegmentedControl titles programmatically

How can i change UISegmentedControl titles programmatically? Could you please help me?

@synthesize filterControl; //UISegmentedControl

- (void)viewDidLoad
{
    [super viewDidLoad];

    filterControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"ALL",@"PROFIT",@"LOSS", nil]]; //not working

}

Upvotes: 3

Views: 5135

Answers (6)

MALIKK HABIB UR REHMAN
MALIKK HABIB UR REHMAN

Reputation: 880

Swift5

To change title font and size for HmSegment control you have to make use of titleTextAttributes property.

segmentedControl.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Poppins-Medium", size: 16.0) as Any]
segmentedControl.setNeedsDisplay()

Upvotes: 0

Kiran Jadhav
Kiran Jadhav

Reputation: 3317

Updated :

Use below simple code to set UISegmentControl title programatically;

- (void)viewDidLoad {

    [super viewDidLoad];

     [self.segmentControl setTitle:@"Love It" forSegmentAtIndex:0];
     [self.segmentControl setTitle:@"Hate It" forSegmentAtIndex:1];        
     [self.segmentControl setTitle:@"Ehh" forSegmentAtIndex:2];
}

// Note: here self.segmentControl = filterControl

Upvotes: 0

samwize
samwize

Reputation: 27363

You can change the sectionTitles directly.

What is often missed out is calling setNeedsDisplay().

The code in Swift:

segmentedControl.sectionTitles = ["All", "Profit", "Loss"]
segmentedControl.setNeedsDisplay()

Upvotes: 2

Ashwini Chougale
Ashwini Chougale

Reputation: 1093

 [filterControl setTitle:@"All" forSegmentAtIndex:0];
 [filterControl setTitle:@"PROFIT" forSegmentAtIndex:1];
 [filterControl  setTitle:@"LOSS" forSegmentAtIndex:2];

try this. hope it will work for you.

Upvotes: 1

olynoise
olynoise

Reputation: 2056

Just use

-setTitle:(NSString*)title forSegmentAtIndex:(NSUInteger)index;

Upvotes: 6

Allamaprabhu
Allamaprabhu

Reputation: 865

Have u added filterControl as subview to its superview?

Upvotes: -1

Related Questions