美文网首页
UIButton以及UISegmentedControl

UIButton以及UISegmentedControl

作者: 888zq | 来源:发表于2018-01-03 14:30 被阅读6次

 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;

}

相关文章

网友评论

      本文标题:UIButton以及UISegmentedControl

      本文链接:https://www.haomeiwen.com/subject/fmyhnxtx.html