
我创建了一个分段控件,它的init函数(如下所示)在包含分段控件的视图控制器类中调用.我能够从分段控件类中删除分段控件的所有边框和分隔符,如下所示:
import Foundationimport UIKitclass CashSegmentedControl: UISegmentedControl{func initUI(){ removeborders()}func removeborders(){ self.tintcolor = UIcolor.clear} 我希望它在每个段下面都有一条线当选择了段时(类似于instagram)
我搜索了很多,并在StackOverflow上发现了一些帖子,但它们似乎是旧版本的Swift.我真的很感激这方面的任何帮助,如果有更好的解决方案来定制边界(除了我所做的),我想要了解更多!
非常感谢 :)
解决方法 在单独的swift文件中添加以下代码(命令N – >新文件):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中仅显示所选项目的底部边框?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)