ios UISegmentedControl的定制

先看效果图

代码:

@property (nonatomic, strong) UISegmentedControl *segmentedControl;


-(void)createSegMentController{
    NSArray *segmentedArray = [NSArray arrayWithObjects:@"通知",@"评论",nil];
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    segmentedControl.frame = CGRectMake(0, 0, 190, 22);
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.tintColor = UIColorFromRGB(0x3E4759);
    segmentedControl.backgroundColor = [UIColor whiteColor];
    segmentedControl.layer.masksToBounds = YES;
    segmentedControl.layer.cornerRadius = 12;
    segmentedControl.layer.borderWidth = 0.5;
    segmentedControl.layer.borderColor = UIColorFromRGB(0x3E4759).CGColor;
    NSDictionary *dicnor = [NSDictionary dictionaryWithObjectsAndKeys:UIColorFromRGB(0x3E4759),NSForegroundColorAttributeName,[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    [segmentedControl setTitleTextAttributes:dicnor forState:UIControlStateNormal];
    NSDictionary *dicselect = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    [segmentedControl setTitleTextAttributes:dicselect forState:UIControlStateSelected];

    [segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    [self.navigationItem setTitleView:segmentedControl];
    self.segmentedControl = segmentedControl;

}

-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender
{
    NSInteger selecIndex = sender.selectedSegmentIndex;
    if (selecIndex == 0) {
        sender.selectedSegmentIndex = 0;
       
    } else {
        sender.selectedSegmentIndex = 1;
       
    }
}
原文地址:https://www.cnblogs.com/qiyiyifan/p/8880723.html