2

如何以编程方式更改 UISegmentedControl 标题?请你帮助我好吗?

@synthesize filterControl; //UISegmentedControl

- (void)viewDidLoad
{
    [super viewDidLoad];

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

}
4

6 回答 6

6

只需使用

-setTitle:(NSString*)title forSegmentAtIndex:(NSUInteger)index;
于 2013-04-24T18:50:45.400 回答
2

可以sectionTitles直接改。

经常错过的是 call setNeedsDisplay()

Swift 中的代码:

segmentedControl.sectionTitles = ["All", "Profit", "Loss"]
segmentedControl.setNeedsDisplay()
于 2017-05-24T05:18:01.407 回答
1
 [filterControl setTitle:@"All" forSegmentAtIndex:0];
 [filterControl setTitle:@"PROFIT" forSegmentAtIndex:1];
 [filterControl  setTitle:@"LOSS" forSegmentAtIndex:2];

尝试这个。希望它对你有用。

于 2016-06-28T06:57:43.107 回答
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

于 2019-05-09T06:56:47.683 回答
0

斯威夫特5

要更改 HmSegment 控件的标题字体和大小,您必须使用 titleTextAttributes 属性。

segmentedControl.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Poppins-Medium", size: 16.0) as Any]
segmentedControl.setNeedsDisplay()
于 2021-06-07T13:07:11.020 回答
-1

您是否已将 filterControl 作为子视图添加到其父视图中?

于 2012-05-09T12:00:54.307 回答