話不多說(shuō),直接上代碼
設(shè)置字體大小和顏色
//使用文本屬性
UIFont *font = [UIFont fontWithName:fontName_bold size:18];
UIColor *textColor = [UIColor whiteColor];
NSDictionary *attributesDict = @{
NSFontAttributeName:font,
NSForegroundColorAttributeName:textColor
};//還可以添加其他屬性
//未選中狀態(tài)
[segment setTitleTextAttributes:attributesDict
forState:UIControlStateNormal];
//選中狀態(tài)
[segment setTitleTextAttributes:attributesDict
forState:UIControlStateSelected];
去邊框(過(guò)期代碼)
// 1.去掉整個(gè)segment顏色,現(xiàn)在整個(gè)segment都看不見(jiàn)
self.segment.tintColor = [UIColor clearColor];
//設(shè)置文字屬性其,把兩個(gè)button的文字顯示出來(lái)
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];
去邊框(正確姿勢(shì))
//通過(guò)設(shè)置背景圖片達(dá)到目的
[segment setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[segment setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
//這個(gè)方法是高亮狀態(tài)的背景圖,就是點(diǎn)擊其他后閃那一下
//[segment setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]
// forState:UIControlStateHighlighted
// barMetrics:UIBarMetricsDefault];
設(shè)置圓角、邊框
//網(wǎng)上方法很多,我這個(gè)前提是去掉了邊框
segment.layer.cornerRadius = 10;
segment.layer.masksToBounds = YES;
segment.layer.borderWidth = 1;
segment.layer.borderColor = [UIColor redColor].CGColor;
去除豎線
//也是通過(guò)設(shè)置圖片達(dá)到目的
[segment setDividerImage:[UIImage imageWithColor:[UIColor clearColor]] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
根據(jù)顏色顏色生成圖片(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;
}
毛玻璃效果(蘋(píng)果自帶的API,有興趣可以去了解)
UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
effectview.backgroundColor = [UIColor whiteColor];
effectview.frame = segment.bounds;
effectview.alpha = 0.1;
[segment addSubview:effectview];
綜合效果
![效果圖][image.png
]