ios UISegmentedControl 用法举例

UISegmentedControl * segmentControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, 160, 30)];

segmentControl.tintColor = kMainOrangeColor;

[segmentControl insertSegmentWithTitle:@"XX" atIndex:0 animated:NO];

[segmentControl insertSegmentWithTitle:@"XX" atIndex:1 animated:NO];

segmentControl.selectedSegmentIndex = 0;

//    [segmentControl setTitle:@"关注" forSegmentAtIndex:0];

//    [segmentControl setTitle:@"订阅" forSegmentAtIndex:1];

self.navigationItem.titleView = segmentControl;

//设置普通状态下(未选中)状态下的文字颜色和字体

//    [self.segmentedControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];

//设置选中状态下的文字颜色和字体

//    [self.segmentedControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateSelected];

//添加监听

[segmentControl addTarget:self action:@selector(sementedControlClick:) forControlEvents:UIControlEventValueChanged];

//监听方法

- (void)sementedControlClick:(UISegmentedControl *)controlllll{

    NSLog(@"%lu ",controlllll.selectedSegmentIndex);

    

}

原文地址:https://www.cnblogs.com/isItOk/p/4873154.html