UISegmentedControl 去边框、去竖线、圆角、字体大小和颜色

话不多说,直接上代码

设置字体大小和颜色

//使用文本属性
UIFont *font = [UIFont fontWithName:fontName_bold size:18];
UIColor *textColor = [UIColor whiteColor];
NSDictionary *attributesDict = @{
                                     NSFontAttributeName:font,
                                     NSForegroundColorAttributeName:textColor
                                     };//还可以添加其他属性
//未选中状态
[segment setTitleTextAttributes:attributesDict
                               forState:UIControlStateNormal];
 //选中状态
 [segment setTitleTextAttributes:attributesDict
                           forState:UIControlStateSelected];

去边框(过期代码)

//    1.去掉整个segment颜色,现在整个segment都看不见
  self.segment.tintColor = [UIColor clearColor];
//设置文字属性其,把两个button的文字显示出来
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],NSForegroundColorAttributeName: [UIColor whiteColor]};
    [ self.segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],NSForegroundColorAttributeName: [UIColor lightTextColor]};
    [self.segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];

去边框(正确姿势)

//通过设置背景图片达到目的
 [segment setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]]
                       forState:UIControlStateNormal
                     barMetrics:UIBarMetricsDefault];
  [segment setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]
                       forState:UIControlStateSelected
                     barMetrics:UIBarMetricsDefault];
//这个方法是高亮状态的背景图,就是点击其他后闪那一下
//[segment setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]
//                       forState:UIControlStateHighlighted
//                     barMetrics:UIBarMetricsDefault];

设置圆角、边框

//网上方法很多,我这个前提是去掉了边框
 segment.layer.cornerRadius = 10;
 segment.layer.masksToBounds = YES;
 segment.layer.borderWidth = 1;
 segment.layer.borderColor = [UIColor redColor].CGColor;

去除竖线

//也是通过设置图片达到目的
 [segment setDividerImage:[UIImage imageWithColor:[UIColor clearColor]] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

根据颜色颜色生成图片(Category)

+ (UIImage *)imageWithColor:(UIColor *)color {
    
    CGRect rect =CGRectMake(0.0f,0.0f, 1.0f,1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context =UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *theImage =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return theImage;
   
}

毛玻璃效果(苹果自带的API,有兴趣可以去了解)

UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectview.backgroundColor = [UIColor whiteColor];
    effectview.frame = segment.bounds;
    effectview.alpha = 0.1;
    [segment addSubview:effectview];

综合效果

![效果图][
image.png

]

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 万事万物有时、有运、有势。 ‘’时”是时机,有天时而运气未至,也难免落空;“运”是天时、地利、人和的和合,三者没和...
    方亚舟阅读 4,411评论 0 1
  • 点绛唇 奉天转凉 暮深城里暖衫薄 对烛孤酌 只影空酬酢 鉴怜经往 稚女求学索 北风恶 漫漫长路 阵阵萧叶落 ...
    宋韻遗风阅读 1,267评论 1 7
  • 只看看的旅游是没有意义的,但带着目的去旅游,或许这就是未来需要的,看一些美的东西,即能赏心悦目也能完全学习到想要学...
    刘小盗阅读 780评论 0 0