Reputation: 719
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
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
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
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
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
Reputation: 2056
Just use
-setTitle:(NSString*)title forSegmentAtIndex:(NSUInteger)index;
Upvotes: 6