大佬教程收集整理的这篇文章主要介绍了ios – 如何在UISegmentedControl中仅显示所选项目的底部边框?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个分段控件,它的init函数(如下所示)在包含分段控件的视图控制器类中调用.我能够从分段控件类中删除分段控件的所有边框和分隔符,如下所示:
import Foundation import UIKit class CashSegmentedControl: UISegmentedControl{ func initUI(){ removeBorders() } func removeBorders(){ self.TintColor = UIColor.clear }
我希望它在每个段下面都有一条线当选择了段时(类似于instagram)
我搜索了很多,并在StackOverflow上发现了一些帖子,但它们似乎是旧版本的Swift.我真的很感激这方面的任何帮助,如果有更好的解决方案来定制边界(除了我所做的),我想要了解更多!
非常感谢 :)
extension UISegmentedControl{ func removeBorder(){ let BACkgroundImage = UIImage.getColoredRectimageWith(color: UIColor.white.cgColor,andSize: self.bounds.sizE) self.setBACkgroundImage(BACkgroundImage,for: .normal,barMetrics: .default) self.setBACkgroundImage(BACkgroundImage,for: .SELEcted,for: .highlighted,barMetrics: .default) let deviderImage = UIImage.getColoredRectimageWith(color: UIColor.white.cgColor,andSize: CGSize(width: 1.0,height: self.bounds.size.height)) self.setDividerImage(deviderImage,forLeftSegmentState: .SELEcted,rightSegmentState: .normal,barMetrics: .default) self.settitleTextAttributes([NSForegroundColorAttributename: UIColor.gray],for: .normal) self.settitleTextAttributes([NSForegroundColorAttributename: UIColor(red: 67/255,green: 129/255,blue: 244/255,alpha: 1.0)],for: .SELEcted) } func addUnderlineForSELEctedSegment(){ removeBorder() let underlineWidth: CGFloat = self.bounds.size.width / CGFloat(self.numberOfSegments) let underlineHeight: CGFloat = 2.0 let underlineXPosition = CGFloat(SELEctedSegmenTindex * Int(underlineWidth)) let underLineYPosition = self.bounds.size.height - 1.0 let underlineFrame = CGRect(x: underlineXPosition,y: underLineYPosition,width: underlineWidth,height: underlineHeight) let underline = UIView(frame: underlineFramE) underline.BACkgroundColor = UIColor(red: 67/255,alpha: 1.0) underline.tag = 1 self.addSubview(underlinE) } func changeUnderlinePosition(){ guard let underline = self.viewWithTag(1) else {return} let underlineFinalXPosition = (self.bounds.width / CGFloat(self.numberOfSegments)) * CGFloat(SELEctedSegmenTindeX) UIView.animate(withDuration: 0.1,animations: { underline.frame.origin.x = underlineFinalXPosition }) } } extension UIImage{ class func getColoredRectimageWith(color: CGColor,andSize size: CGSizE) -> UIImage{ UIGraphicsBeginImageContextWithOptions(size,false,0.0) let graphicsContext = UIGraphicsGetCurrentContext() graphicsContext?.setFillColor(color) let rectangle = CGRect(x: 0.0,y: 0.0,width: size.width,height: size.height) graphicsContext?.fill(rectanglE) let rectangleImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return rectangleImage! } }
然后从viewDidLoad()方法调用segmentedControl.addUnderlineForSELEctedSegment()之后,为分段控件创建一个@IBACtion方法,如下所示:
@IBACtion func segmentedControlDidChange(_ sender: UISegmentedControl){ segmentedControl.changeUnderlinePosition() }
然后从此方法中调用segmentedControl.changeUnderlinePosition().
不要忘记将故事板中的分段控件连接到刚刚创建的@IBACtion方法.
非常重要:不要忘记在故事板中使用自动布局来确定分段控件的大小和位置.
这是结果:
随意问你可能有的任何其他问题:)
以上是大佬教程为你收集整理的ios – 如何在UISegmentedControl中仅显示所选项目的底部边框?全部内容,希望文章能够帮你解决ios – 如何在UISegmentedControl中仅显示所选项目的底部边框?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。