18

I'm extremely new to iOS development and ran into some trouble while building an app for a course.

I created a segmented control and its init function (shown below) is being called in the view controller class containing the segmented control. I was able to remove all borders and dividers of the segmented control from the segmented control class as follows:

import Foundation
import UIKit

class CashSegmentedControl: UISegmentedControl{

func initUI(){
    removeBorders()
}

func removeBorders(){
    self.tintColor = UIColor.clear

}

This is what my result looks like currently

I want it to have a line under each segment WHEN the segment is selected (similar to instagram)

This is a sample of what I want it to look like

I've searched a lot and come across some posts on StackOverflow but they seem to be for older versions of Swift. I'd really appreciate any help in this matter, and if there is a better solution for customising the borders (other than what I have done), I'd love to learn more!

Thanks a lot :)

Praveen Gowda I V
  • 9,569
  • 4
  • 41
  • 49
Richa Netto
  • 305
  • 2
  • 4
  • 8
  • @TusharSharma I've tried the answer below by Gabrail but there's a slight problem with it. Do you have any other suggestions? Thanks! – Richa Netto Mar 13 '17 at 04:11
  • UISegmented customisation bit complex. you could use third party code or simply use 2 buttons and 2 UIView with blue background and show hide accordingly. – Avijit Nagare Mar 13 '17 at 07:04
  • Have a look: https://github.com/cwRichardKim/RKSwipeBetweenViewControllers – Avijit Nagare Mar 13 '17 at 07:16

6 Answers6

53

Add the following code in a separate swift file (command+N -> New File):

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, barMetrics: .default)
        self.setBackgroundImage(backgroundImage, 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, green: 129/255, blue: 244/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!
    }
}

Then after call segmentedControl.addUnderlineForSelectedSegment() from your viewDidLoad() method, and create an @IBAction method for the segmented control like so:

@IBAction func segmentedControlDidChange(_ sender: UISegmentedControl){
        segmentedControl.changeUnderlinePosition()
    }

Then call segmentedControl.changeUnderlinePosition() from within this method.

Do not forget to connect the segmented control from your storyboard to the @IBAction method you just created.

Very important: Don't forget to use Auto layout in the storyboard to determine the size and position of your segmented control.

This is the result:

enter image description here

enter image description here

enter image description here

Feel free to ask any other questions you may have :)

fja
  • 1,823
  • 1
  • 15
  • 28
  • Thank you so much! This works perfectly except for a small glitch that seems to pop up when I move from the third segment to the first segment. When I click segment 3, and then click segment 1, there is a double underline for the first segment for some reason. This doesn't show up in any other transition. I'm not sure where this might be happening in the code so I'm still checking, but if you have any input, let me know! Thanks again :) your code is great! – Richa Netto Mar 13 '17 at 23:04
  • @RichaNetto did you use Auto Layout in the storyboard to determine the segmented control's size and position? – fja Mar 13 '17 at 23:31
  • I did this: Selected Segmented Control in storyboard -> Editor -> Resolve Auto Layout Issues -> Reset to Suggested Constraints. Is that what you're talking about? – Richa Netto Mar 13 '17 at 23:41
  • 2
    You need to explicitly set the width of the segmented control: on the bottom right of the storyboard there are four Auto Layout tools, you want the one called `Add New Constraints`, if you hover over them, you'll fine it, the icon is a square between two lines. Click that, then *uncheck* `Constrain to margins`, select the top and left red dotted lines (enter how far you want it to be from the top and left), *check* the `width` and `height` option (enter the width and height you want), and finally from 'Update Frames' select `Items of New Constraint` and click `Add Constraints`. @RichaNetto – fja Mar 14 '17 at 00:09
  • @A.Jam I have above code. its working perfectly but facing one minor issue. Actully i have 2 segments. & but segment 1 underline is having height is less than 1 pixel that segment 2. why its happening.? Can you please advice? – Kalyani May 03 '17 at 11:09
  • @Kalyani I am going to guess it is most probably because you are either not setting the constraints required to determine the size and position of the segmented control, or you are setting the constraints incorrectly. The original poster also had a similar problem if you look thought the comments, I explain how to set your constraints. – fja May 03 '17 at 12:18
  • I have use the same code and it works fine for segment controller but when i rotate my device that it will draw bottom line properly. Please suggest something for it – Anjan Feb 01 '18 at 12:26
  • 1
    The code above works for the most part but I'm having a strange problem. ONLY on the first segment, the underline won't be the topmost view. I've opened it in debug view hierarachy and I see it between the actual UISegmentControl and the UISegment CALayer. But only on the first segment. If I click the 2nd item in the segment control, it works fine. Has anyone seen this? – Shawn Mar 10 '18 at 17:15
  • Add this under the animate in changeUnderlinePosition: underline.layer.zPosition = 1 (or otherwise flip the view to top...see https://stackoverflow.com/questions/4631878/how-to-set-iphone-uiview-z-index) – codeslapper Jul 25 '18 at 18:50
  • It's a lifesaver code. Thanks @A.Jam.. I just want to ask, that I want to scroll the line according to movement of pageviewcontroller,, please help. – Pratyush Pratik Nov 12 '18 at 14:09
  • I suggest to change: let underlineWidth: CGFloat = self.bounds.size.width / CGFloat(self.numberOfSegments) by: let underlineWidth: CGFloat = UIScreen.main.bounds.width / CGFloat(self.numberOfSegments) – Niib Fouda Jan 07 '19 at 14:16
  • @A.Jam This is working for me but my issue is I'm setting segmentControl.apportionsSegmentWidthsByContent = true now I am not getting the underline based on the segment content size. underline width is taking the default width of the segment. Do you have any idea how to overcome this issue?? – Venkatesh Chejarla Jan 08 '19 at 13:13
  • getting underline height different - different for 1st and 2nd segment – Pramod Shukla Feb 06 '19 at 06:36
  • @VenkateshChejarla Try Niib Fouda's comment change: let underlineWidth: CGFloat = self.bounds.size.width / CGFloat(self.numberOfSegments) to: let underlineWidth: CGFloat = UIScreen.main.bounds.width / CGFloat(self.numberOfSegments) This worked for me. – mikemike396 May 08 '19 at 18:03
  • @A.Jam Thanks for awesome extension! There was one problem: when I pressed selected index I seen dark highlight overlay on it. Fixed by adding: `self.setBackgroundImage(backgroundImage, for: UIControl.State.selected.union(.highlighted), barMetrics: .default)` to `removeBorder` method. – landonandrey Aug 01 '19 at 15:26
10

A.Jam's answer in Xcode 9.3, Swift 4.2 version

import UIKit

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, barMetrics: .default)
        self.setBackgroundImage(backgroundImage, 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([NSAttributedStringKey.foregroundColor: UIColor.gray], for: .normal)
        self.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: 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, green: 129/255, blue: 244/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!
    }
}
swift2geek
  • 1,697
  • 1
  • 20
  • 27
  • This is working for me but my issue is I'm setting `segmentControl.apportionsSegmentWidthsByContent = true` now I am not getting the underline based on the segment content size. underline width is taking the default width of the segment. Do you have any idea how to overcome this issue?? – Venkatesh Chejarla Jan 08 '19 at 12:53
  • 2
    It seems to be impossible to remove the vertical divider when using a black background. Removing "setDividerImage" has no effect. I still have a white vertical divider... – lucius degeer May 02 '19 at 03:09
2

you can use this

let segmentBottomBorder = CALayer()
segmentBottomBorder?.borderColor = UIColor.blue.cgColor
segmentBottomBorder?.borderWidth = 3
let x = CGFloat(sender.selectedSegmentIndex) * width
let y = sender.frame.size.height - (segmentBottomBorder?.borderWidth)!
let width: CGFloat = sender.frame.size.width/3
segmentBottomBorder?.frame = CGRect(x: x, y: y, width: width, height: (segmentBottomBorder?.borderWidth)!)
sender.layer.addSublayer(segmentBottomBorder!)
Gabrail
  • 216
  • 2
  • 16
  • Hi, thank you for your input! This gets a border line at the bottom for the first selected item (item 1) segment, but if I try to switch to another item in the segment, a bottom border appears for the newly selected item (item 2) and ALSO remains on the previously selected item 1. So basically the bottom line is not being updated for the previously selected item (it should appear to be deselected). – Richa Netto Mar 13 '17 at 04:09
  • you set segmentBottomBorder as global variable and each time you select segment call this method with segmentBottomBorder?.removeFromSuperlayer() – Gabrail Mar 13 '17 at 11:06
  • @Gabrail what is the `width` in `let x = CGFloat(sender.selectedSegmentIndex) * width` – Venkatesh Chejarla Jan 08 '19 at 13:28
2

A.Jam's answer in Xcode 10.1, Swift 4.2 version - without UIImage Extension

and a grey underline for all segmentIndexes

extension UISegmentedControl {

  func removeBorder(){

    self.tintColor = UIColor.clear
    self.backgroundColor = UIColor.clear
    self.setTitleTextAttributes( [NSAttributedString.Key.foregroundColor : UIColor.orange], 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.gray

    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.orange
    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

  }
}
DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
Kobi
  • 89
  • 5
1

To create WHITE segmented control in iOS13

segmentedControl.setTitleTextAttributes( [NSAttributedString.Key.foregroundColor : UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .medium)], for: .selected)
segmentedControl.setTitleTextAttributes( [NSAttributedString.Key.foregroundColor : UIColor.gray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .medium)], for: .normal)
    if #available(iOS 13.0, *) {
        let dividerImage = UIImage(color: .white, size: CGSize(width: 1, height: 32))
        segmentedControl.setBackgroundImage(UIImage(named: "w1"), for: .normal, barMetrics: .default)
        segmentedControl.setBackgroundImage(UIImage(named: "w2"), for: .selected, barMetrics: .default)
        segmentedControl.setDividerImage(dividerImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
        segmentedControl.layer.cornerRadius = 0
        segmentedControl.layer.masksToBounds = true
    }

extension UIImage {
    convenience init(color: UIColor, size: CGSize) {
        UIGraphicsBeginImageContextWithOptions(size, false, 1)
        color.set()
        let ctx = UIGraphicsGetCurrentContext()!
        ctx.fill(CGRect(origin: .zero, size: size))
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        self.init(data: image.pngData()!)!
    }
}

Where w1 image is enter image description here and w2 image is enter image description here

A.Jams answer for SWIFT 5.1 Xcode 11.3 to create GRAY control

import Foundation
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)
        if #available(iOS 13.0, *) {
            self.selectedSegmentTintColor = UIColor.clear
        } 

    }

    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

    }
}
Dmih
  • 530
  • 6
  • 9
0

My own implementation of this using the accepted Answer and some tidbits from others.

Changes:

  1. it will honour Dark / Light mode appearance changes and the segmentedControl TextColor and BGColor will change accordingly. This will also get rid of the segmented control dividing lines.
  2. I wrapped the setupSegment() and addUnderlineForSelectedSegment() within DispatchQueue.main.asyn() so that changes to phone orientation will get the correct width.
  3. Added a new function called removeUnderline() to get rid of the previous underline before drawing a new one. This is so that the underline width will correspond to the actual width within that orientation.

Where to put stuffs.

viewDidLoad() {
  yourUISegmentedControl.setupSegment()
}

  /// https://stackoverflow.com/a/57943610/14414215
  /// Use this to detect changes in User Interface (Dark or Light Mode) then setup the UISegmentedControl Appearance
  override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    yourUISegmentedControl.setupSegment()
  }

Note: I did try to put into viewWillTransition() but this will only detect phone orientation changes and not Dark/Light mode appearance changes. Putting it into traitCollectionDidChange() will actually cater for both Dark/Light Mode & Orientation Changes.

The rest below you can just put Into it's own Swift File.

import UIKit

extension UISegmentedControl {
  
  func removeBorder(){
    var bgcolor: CGColor
    var textColorNormal: UIColor
    var textColorSelected: UIColor
    
    if self.traitCollection.userInterfaceStyle == .dark {
      bgcolor = UIColor.black.cgColor
      textColorNormal = UIColor.gray
      textColorSelected = UIColor.white
    } else {
      bgcolor = UIColor.white.cgColor
      textColorNormal = UIColor.gray
      textColorSelected = UIColor.black
    }
    
    let backgroundImage = UIImage.getColoredRectImageWith(color: bgcolor, andSize: self.bounds.size)
    self.setBackgroundImage(backgroundImage, for: .normal, barMetrics: .default)
    self.setBackgroundImage(backgroundImage, for: .selected, barMetrics: .default)
    self.setBackgroundImage(backgroundImage, for: .highlighted, barMetrics: .default)
    
    let deviderImage = UIImage.getColoredRectImageWith(color: bgcolor, andSize: CGSize(width: 1.0, height: self.bounds.size.height))
    self.setDividerImage(deviderImage, forLeftSegmentState: .selected, rightSegmentState: .normal, barMetrics: .default)
    self.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: textColorNormal], for: .normal)
    self.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: textColorSelected], for: .selected)
    
  }
  
  func setupSegment() {
    DispatchQueue.main.async() {

    self.removeBorder()
// I Commented all these out as I didn't see what exactly it's used for as the color is clear anyways
//    let segmentUnderlineWidth: CGFloat = self.bounds.width
//    print("setupSegment UnderlineWidth:\(segmentUnderlineWidth) width:\(UIScreen.main.bounds.width) \(self.bounds.size.width)")
//    let segmentUnderlineHeight: CGFloat = 10.0
//    let segmentUnderlineXPosition = self.bounds.minX
//    let segmentUnderLineYPosition = self.bounds.size.height - 4.0
//    let segmentUnderlineFrame = CGRect(x: segmentUnderlineXPosition, y: segmentUnderLineYPosition, width: segmentUnderlineWidth, height: segmentUnderlineHeight)
//    let segmentUnderline = UIView(frame: segmentUnderlineFrame)
//    segmentUnderline.backgroundColor = UIColor.red
//
//    self.addSubview(segmentUnderline)
    self.addUnderlineForSelectedSegment()
    }
  }
  
  func addUnderlineForSelectedSegment(){
    DispatchQueue.main.async() {
      self.removeUnderline()
      let underlineWidth: CGFloat = self.bounds.size.width / CGFloat(self.numberOfSegments)
      let underlineHeight: CGFloat = 10.0
      let underlineXPosition = CGFloat(self.selectedSegmentIndex * Int(underlineWidth))
      let underLineYPosition = self.bounds.size.height - 4.0
      let underlineFrame = CGRect(x: underlineXPosition, y: underLineYPosition, width: underlineWidth, height: underlineHeight)
      let underline = UIView(frame: underlineFrame)
      underline.backgroundColor = UIColor.darkGray
      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
  }
  
  func removeUnderline(){
    guard let underline = self.viewWithTag(1) else {return}
    underline.removeFromSuperview()
  }
}


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!
  }
}
app4g
  • 670
  • 4
  • 24