完全删除UISegmentedControl分隔符。 (苹果手机)

有没有一种方法可以完全删除UISegmentedControl中分隔两个线段的线?

设置segmentedControlStyle没有帮助。

不,那里没有。 您应该考虑手动创buildUISegmentedControlfunction。 也许使用两个UIButtons。

如果有人想要这种UISegmentedControl View

在这里输入图像说明

编辑: – Swift 3.0扩展

  extension UISegmentedControl { func customizeAppearance(for height: Int) { setTitleTextAttributes([NSFontAttributeName:UIFont(name:"Helvetica Neue", size:13.0)!,NSForegroundColorAttributeName:UIColor.white], for:.normal) setTitleTextAttributes([NSFontAttributeName:UIFont(name:"Helvetica Neue", size:13.0)!,NSForegroundColorAttributeName:UIColor.white], for:.selected) setDividerImage(UIImage().colored(with: .clear, size: CGSize(width: 1, height: height)), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default) setBackgroundImage(UIImage().colored(with: .clear, size: CGSize(width: 1, height: height)), for: .normal, barMetrics: .default) setBackgroundImage(UIImage().colored(with: UIColor.init(red: 215/255.0, green: 0.0, blue: 30/255.0, alpha: 1.0), size: CGSize(width: 1, height: height)), for: .selected, barMetrics: .default); for borderview in subviews { let upperBorder: CALayer = CALayer() upperBorder.backgroundColor = UIColor.init(red: 215/255.0, green: 0.0, blue: 30/255.0, alpha: 1.0).cgColor upperBorder.frame = CGRect(x: 0, y: borderview.frame.size.height-1, width: borderview.frame.size.width, height: 1) borderview.layer.addSublayer(upperBorder) } } } extension UIImage { func colored(with color: UIColor, size: CGSize) -> UIImage { UIGraphicsBeginImageContext(size) let context = UIGraphicsGetCurrentContext() context!.setFillColor(color.cgColor); let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: size) context!.fill(rect); let image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image! } } 

UIImage扩展生成具有所需大小的彩色图像。

在构buildcontext时,强制customAppearance中的height参数customizeAppearance避免任何崩溃。

通过提取每个定制属性并将其作为函数的参数,可以使其更加可重用;)

这里是示例代码,通过iOS 9testing,并为我工作的很好。

viewDidLoad添加这些代码行 –

 [yourSegmentControl setTitleTextAttributes:@{ NSFontAttributeName:[UIFont fontWithName:@"Roboto-black" size:13.0],NSForegroundColorAttributeName:[UIColor whiteColor] }forState:UIControlStateSelected]; [yourSegmentControl setTitleTextAttributes:@{ NSFontAttributeName:[UIFont fontWithName:@"Roboto-black" size:13.0],NSForegroundColorAttributeName:[UIColor whiteColor] }forState:UIControlStateNormal]; [yourSegmentControl setDividerImage:[self imageWithColor:[UIColor clearColor]] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [yourSegmentControl setBackgroundImage:[self imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [yourSegmentControl setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:215/255.0 green:0 blue:30/255.0 alpha:1.0]] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; for (UIView *borderview in yourSegmentControl.subviews) { CALayer *upperBorder = [CALayer layer]; upperBorder.backgroundColor = [UIColor colorWithRed:215/255.0 green:0 blue:30/255.0 alpha:1.0].CGColor; upperBorder.frame = CGRectMake(0, borderview.frame.size.height-1, borderview.frame.size.width, 1.0f); [borderview.layer addSublayer:upperBorder]; } 

 - (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 *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 

这里是Swift扩展实现相同的视图,

 yourSegmentControl.setTitleTextAttributes([NSFontAttributeName:UIFont(name:"Helvetica Neue", size:13.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Normal) yourSegmentControl.setTitleTextAttributes([NSFontAttributeName:UIFont(name:"Helvetica Neue", size:13.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Selected) yourSegmentControl.setDividerImage(self.imageWithColor(UIColor.clearColor()), forLeftSegmentState: UIControlState.Normal, rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default) yourSegmentControl.setBackgroundImage(self.imageWithColor(UIColor.clearColor()), forState:UIControlState.Normal, barMetrics:UIBarMetrics.Default) yourSegmentControl.setBackgroundImage(self.imageWithColor(UIColor.init(red: 215/255.0, green: 0.0, blue: 30/255.0, alpha:1.0)), forState:UIControlState.Selected, barMetrics:UIBarMetrics.Default); for borderview in yourSegmentControl.subviews { let upperBorder: CALayer = CALayer() upperBorder.backgroundColor = UIColor.init(red: 215/255.0, green: 0.0, blue: 30/255.0, alpha: 1.0).CGColor upperBorder.frame = CGRectMake(0, borderview.frame.size.height-1, borderview.frame.size.width, 1.0); borderview.layer .addSublayer(upperBorder); } 

 func imageWithColor(color: UIColor) -> UIImage { let rect = CGRectMake(0.0, 0.0, 1.0, yourSegmentControl.frame.size.height) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() CGContextSetFillColorWithColor(context, color.CGColor); CGContextFillRect(context, rect); let image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image } 

使用这种方法来改变你的段分隔图像:

 [segmentControl setDividerImage:dividerimg forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 

我把以前的答案合并成一个Swift扩展。

我想删除所有的边界和分隔线。 这段代码只是使用正常的段颜色,例如色调和背景来select/取消select。

呼叫

 segmentedControl.removeBorders() 

这是扩展名

 extension UISegmentedControl { func removeBorders() { setBackgroundImage(imageWithColor(backgroundColor!), forState: .Normal, barMetrics: .Default) setBackgroundImage(imageWithColor(tintColor!), forState: .Selected, barMetrics: .Default) setDividerImage(imageWithColor(UIColor.clearColor()), forLeftSegmentState: .Normal, rightSegmentState: .Normal, barMetrics: .Default) } // create a 1x1 image with this color private func imageWithColor(color: UIColor) -> UIImage { let rect = CGRectMake(0.0, 0.0, 1.0, 1.0) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() CGContextSetFillColorWithColor(context, color.CGColor); CGContextFillRect(context, rect); let image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image } } 

下面的代码将完成你想要的。

 UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, self.segControlOut.frame.size.height), NO, 0.0); UIImage *blank = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [self.segControlOut setDividerImage:blank forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 

您可以通过设置黑色图像来从UISegmentedControl删除所有背景,边框和分隔符。 或者你可以设置自定义颜色。

下面的例子将删除所有边界/分隔符,并将背景设置为0.2 alpha的白色,并将选定的分段设置为alpha 1.0的白色

 [self.segmentedControl setBackgroundImage:[self imageWithColor:[UIColor colorWithWhite:1.0 alpha:0.2]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [self.segmentedControl setBackgroundImage:[self imageWithColor:[UIColor colorWithWhite:1.0 alpha:1.0]] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; - (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 *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 

我已经使用segmentcontrol.tintColor = [UIColor clearColor]; 与iOS7,它似乎工作。

@ naveed's和@Jon Setting的答案拯救了我的一天!

但作为一个完美主义者,我很沮丧地发现,这个代码也留下了控制的外部边界的漏洞:

之前

所以我添加了几行代码来摆脱这些,并使其看起来整洁: 后

完整的代码将是

 CGFloat h = self.segControlOut.frame.size.height; UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, h), NO, 0.0); // draw the missing dots with the control border color [self.segControlOut.tintColor set]; UIRectFrame( CGRectMake(0, -1, 1, 2.5) ); UIRectFrame( CGRectMake(0, h-1.5, 1, 2.5) ); UIImage *blank = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [self.segControlOut setDividerImage:blank forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
 - (void) configSegmentControl { [segmentControl setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [segmentControl setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; segmentControl.layer.cornerRadius = 4.0f; segmentControl.clipsToBounds = YES; } - (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 *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }