如何以编程方式更改UISegmentedControl标题?你能帮我吗?
@synthesize filterControl; //UISegmentedControl
- (void)viewDidLoad
{
[super viewDidLoad];
filterControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"ALL",@"PROFIT",@"LOSS", nil]]; //not working
}
答案 0 :(得分:6)
只需使用
-setTitle:(NSString*)title forSegmentAtIndex:(NSUInteger)index;
答案 1 :(得分:2)
您可以直接更改sectionTitles
。
经常错过的是致电setNeedsDisplay()
。
Swift中的代码:
segmentedControl.sectionTitles = ["All", "Profit", "Loss"]
segmentedControl.setNeedsDisplay()
答案 2 :(得分:1)
[filterControl setTitle:@"All" forSegmentAtIndex:0];
[filterControl setTitle:@"PROFIT" forSegmentAtIndex:1];
[filterControl setTitle:@"LOSS" forSegmentAtIndex:2];
试试这个。希望它对你有用。
答案 3 :(得分:0)
已更新:
使用下面的简单代码以编程方式设置UISegmentControl标题;
- (void)viewDidLoad {
[super viewDidLoad];
[self.segmentControl setTitle:@"Love It" forSegmentAtIndex:0];
[self.segmentControl setTitle:@"Hate It" forSegmentAtIndex:1];
[self.segmentControl setTitle:@"Ehh" forSegmentAtIndex:2];
}
//注意:此处self.segmentControl = filterControl
答案 4 :(得分:0)
Swift5
要更改 HmSegment 控件的标题字体和大小,您必须使用 titleTextAttributes 属性。
segmentedControl.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Poppins-Medium", size: 16.0) as Any]
segmentedControl.setNeedsDisplay()
答案 5 :(得分:-1)
您是否已将filterControl作为子视图添加到其superview中?