Created
April 24, 2019 11:25
Revisions
-
Denismih created this gist
Apr 24, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ extension UISegmentedControl { func removeBorder(){ self.tintColor = UIColor.clear self.backgroundColor = UIColor.clear self.setTitleTextAttributes( [NSAttributedString.Key.foregroundColor : UIColor.stavkrugDarkBlue], for: .selected) self.setTitleTextAttributes( [NSAttributedString.Key.foregroundColor : UIColor.gray], for: .normal) } func setupSegment() { self.removeBorder() let segmentUnderlineWidth: CGFloat = self.bounds.width let segmentUnderlineHeight: CGFloat = 2.0 let segmentUnderlineXPosition = self.bounds.minX let segmentUnderLineYPosition = self.bounds.size.height - 1.0 let segmentUnderlineFrame = CGRect(x: segmentUnderlineXPosition, y: segmentUnderLineYPosition, width: segmentUnderlineWidth, height: segmentUnderlineHeight) let segmentUnderline = UIView(frame: segmentUnderlineFrame) segmentUnderline.backgroundColor = UIColor.clear self.addSubview(segmentUnderline) self.addUnderlineForSelectedSegment() } func addUnderlineForSelectedSegment(){ 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.stavkrugDarkBlue 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) underline.frame.origin.x = underlineFinalXPosition } }