UIButton以及UISegmentedControl

 self.segment = [[UISegmentedControl alloc]initWithItems:@[@"首付",@"贷款"]];

    //默认值为NO,即点击后保持选中状态,如果设置为YES则,只是短暂的显示选中状态

    self.segment.momentary=NO;

    self.segment.tintColor = [UIColor blackColor];

//    self.segment.backgroundColor = [UIColor whiteColor];  //

    self.segment.layer.masksToBounds=YES;              //    默认为no,不设置则下面一句无效

    self.segment.layer.cornerRadius=5.0;              //    设置圆角大小,同UIView

    self.segment.layer.borderWidth = 1;                  //    边框宽度,重新画边框,若不重新画,可能会出现圆角处无边框的情况

    self.segment.layer.borderColor = [UIColor blackColor].CGColor; //    边框颜色

    [self.segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

    [self.contentView addSubview:self.segment];


    NSDictionary * selectedTextAttributes =@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor whiteColor]};

    [self.segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性

    NSDictionary* unselectedTextAttributes =@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor blackColor]};

    [self.segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];

    [self.segment setBackgroundImage:[self buttonImageFromColor:IWColor(13, 111, 4)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

    [self.segment setBackgroundImage:[self buttonImageFromColor:[UIColor whiteColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];



//通过颜色来生成一个纯色图片

- (UIImage*)buttonImageFromColor:(UIColor*)color{

    CGRectrect =CGRectMake(0,0,40,35);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    returnimg;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容