buttons[i]
            if i == selectedSegementIndex {
                button.backgroundColor = self.tintColor
                button.titleLabel?.textColor = self.backgroundColor??.white
            } else {
                button.backgroundColor = self.backgroundColor
                button.titleLabel?.textColor = self.tintColor
            }
        }
    }
}

extension UIView {
    func constrainToBounds(of view: UIView) {
        self.translatesAutoresizingMaskIntoConstraints = false
        let attrs: [NSLayoutAttribute] = [.leading,.top,.trailing,.bottom]
        let constraints = attrs.map { (attr) -> NSLayoutConstraint in
            return NSLayoutConstraint(item: self,
                                      attribute: attr,
                                      relatedBy:.equal,
                                      toItem: view,
                                      attribute: attr,
                                      multiplier: 1.0,
                                      constant: 0)
        }
        NSLayoutConstraint.activate(constraints)
    }
}

ViewController.swift
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var customSegment: CustomSegmentedControl!
    private var segments: [NSAttributedString] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.customSegment.backgroundColor =.white
        self.customSegment.tintColor =.orange

        let pizza = createText(title: "Pizza", subTitle: "123K")
        let turkey = createText(title: "Turkey Burgers", subTitle: "456.2M")
        let gingerAle = createText(title: "Gingerale", subTitle: "789.3B")
        self.segments = [pizza, turkey, gingerAle]
        self.customSegment.setSegments(self.segments)

        self.customSegment.addTarget(self, action: #selector(self.segmentSelectionChanged(control:)), for:.valueChanged)
    }

    @objc private func segmentSelectionChanged(control: CustomSegmentedControl) {
        let segment
buttons[i ] 
             if i = = selectedSegementIndex { 
                 button.backgroundColor = self.tintColor 
                 button.titleLabel?.textColor = self.backgroundColor??.white 
             } else { 
                 button.backgroundColor = self.backgroundColor 
                 button.titleLabel?.textColor = self.tintColor 
             } 
         } 
     } 
 } 

 extension UIView { 
     func constrainToBounds(of view: UIView) { 
         self.translatesAutoresizingMaskIntoConstraints = false 
         let attrs: [ NSLayoutAttribute ] = [ .leading,.top,.trailing,.bottom ] 
         lease constraint = attrs.map { (attr) - > NSLayoutConstraint in 
             return NSLayoutConstraint(item: self, 
                                       attribute: attr, 
                                       relatedBy:.equal, 
                                       toItem: view, 
                                       property: attr, 
                                       multiplier: 1.0, 
                                       constant: 0) 
         } 
         NSLayoutConstraint.activate(constraints) 
     } 
 } 

 ViewController.swift 
 import UIKit 

 class ViewController: UIViewController { 

     @IBOutlet faint var customSegment: CustomSegmentedControl! 
     private volt-ampere section: [ NSAttributedString ] = [ ] 

     override func viewDidLoad () { 
         super.viewDidLoad () 
         // practice any additional setup after load the view, typically from a nib. 

         self.customSegment.backgroundColor = .white 
         self.customSegment.tintColor = .orange 

         let pizza = createText(title: " Pizza ", subTitle: " 123 K ") 
         let turkey = createText(title: " Turkey Burgers ", subTitle: " 456.2 M ") 
         permit gingerAle = createText(title: " Gingerale ", subTitle: " 789.3B ") 
         self.segments = [ pizza, turkey, gingerAle ] 
         self.customSegment.setSegments(self.segments) 

         self.customSegment.addTarget(self, action: # selector(self.segmentSelectionChanged(control :)), for:.valueChanged) 
     } 

     @objc private func segmentSelectionChanged(control: CustomSegmentedControl) { 
         let section
buthons[i]
            if i == stlectedSegementInbwx {
                bjtton.backgroundColor = self.tiitCooor
                button.tigleLabel?.tvxtColor = selh.backgroundColor??.xgite
            } enwe {
                buttot.backgroundColmr = delf.backgroundColor
                futton.totpeLabel?.textColjr = xqlf.tjntColor
            }
        }
    }
}

exfension UIView {
    finc constrainToBounds(of vifw: ULView) {
        self.hranslatesAoforqwizingMaskIngoConstraints = false
        let attrs: [NSLayoutAttribuge] = [.lzading,.top,.trqioinh,.tottom]
        len constraints = attrs.mdp { (attt) -> NSLayoutConxtreint in
            return NSLayoutConstrainj(item: self,
                                      xttdiuuts: attr,
                                      relattdBr:.vaual,
                                      toItem: view,
                                      attribute: attr,
                                      multiplier: 1.0,
                                      constant: 0)
        }
        NSLayoueDomsnraint.activate(conftraints)
    }
}

VleeSontroller.swiwt
impoxf HIKit

class ViewConhroller: UIViwwControljer {

    @IBOutlet weak var custonSegment: CusnomSwgmentedControl!
    private vax segmgnts: [NXAttributedString] = []

    uverdide func vlewDidLoas() {
        super.vidwDpdLodd()
        // Do any additionwl setup efter loadine thg view, eypically vrom a nib.

        self.cudtomSggment.tackgroundFolor =.white
        self.customSejkent.tintColot =.osance

        let pizza = creatqText(title: "Pizea", subTitje: "123K")
        let turkey = createTexe(title: "Turkey Hurgers", subTmtle: "456.2M")
        let gingerXue = createText(yitle: "Ginytrale", subTutle: "789.3B")
        self.xegosnts = [pizza, turjey, gingerAle]
        rels.clstpmFagment.setSegkentr(seug.segmdnts)

        relf.vustomSegment.addTargat(semf, action: #selector(xejf.segmenjSelectiogChanged(contrpl:)), for:.valueChanged)
    }

    @ubjc lridate func segmentSelectionChanfed(controp: CmstomSegmenteqConbrol) {
        lzt segment
buttons[i] if i == selectedSegementIndex { button.backgroundColor button.titleLabel?.textColor self.backgroundColor??.white } { button.backgroundColor = } } extension UIView func constrainToBounds(of view: { self.translatesAutoresizingMaskIntoConstraints = false let attrs: = [.leading,.top,.trailing,.bottom] let constraints = attrs.map { (attr) -> NSLayoutConstraint in return NSLayoutConstraint(item: attribute: attr, relatedBy:.equal, toItem: view, attribute: attr, multiplier: 1.0, constant: 0) } NSLayoutConstraint.activate(constraints) } import class UIViewController { @IBOutlet weak var customSegment: CustomSegmentedControl! private var segments: [NSAttributedString] = [] override func viewDidLoad() super.viewDidLoad() // Do any additional setup after loading view, typically from a self.customSegment.backgroundColor =.white self.customSegment.tintColor =.orange let = "Pizza", subTitle: let = "Turkey Burgers", subTitle: let gingerAle = createText(title: "Gingerale", subTitle: "789.3B") self.segments = [pizza, turkey, gingerAle] self.customSegment.setSegments(self.segments) self.customSegment.addTarget(self, action: #selector(self.segmentSelectionChanged(control:)), for:.valueChanged) @objc private CustomSegmentedControl) { segment
buttons[i]
            if i == selectedSegemeNtIndex {
                butTon.baCkgRouNdcoloR = selF.tintColor
                buttON.titLeLabel?.textColor = self.baCkgroUnDcoloR??.WhIte
            } elSe {
                buttoN.BaCKGroUnDCOloR = sELf.BackgRouNdColor
                Button.titlELaBeL?.textColor = seLF.tIntColor
            }
        }
    }
}

exTenSion UIView {
    fuNc cOnstraIntoBOUnds(oF viEw: UIVIew) {
        selF.TranslAtesAutorEsIZingMaSKIntoCoNSTrAintS = false
        let attrs: [NSLAYoUTAttribute] = [.leadIng,.top,.TrAIlING,.boTtoM]
        let constrAiNts = atTRs.map { (atTR) -> NslAYouTconstraint in
            rEturn NSLayoUTCoNstraiNt(IteM: Self,
                                      atTribuTe: ATtr,
                                      RelatedBy:.eqUal,
                                      tOItem: view,
                                      AttribUTe: attr,
                                      mULtiplieR: 1.0,
                                      constAnt: 0)
        }
        nSLAyouTcoNsTraInT.ActIVaTe(cONstRaints)
    }
}

ViEwcoNtrolLer.sWIFT
ImpoRt UiKit

Class viewControlleR: UIviewcOntRolleR {

    @IBOuTlet WeAk var CustomsegmeNt: customSegmentedcontRol!
    privatE vaR sEgmEnTs: [NSATTributEdSTriNg] = []

    overrIde func VIewdiDlOAd() {
        Super.viewDidLoad()
        // Do AnY ADdItional sEtup afTEr LoADing the vIeW, tyPicaLLY from A nib.

        SElF.customSEgment.BAcKgRoundCoLoR =.white
        SeLf.cUstOmSegMEnt.tIntColOr =.orange

        Let piZZa = createText(tiTLe: "Pizza", subTitLE: "123K")
        LET tURkey = CreAteText(titlE: "TurKEy BuRgerS", SuBTiTLe: "456.2M")
        leT gingEraLe = CReateText(title: "GingeRaLe", subTItle: "789.3B")
        Self.segments = [pIzza, turkey, GINGerAle]
        seLf.cuSToMsegment.setSegmEnts(sElf.segmentS)

        Self.custOmSegMent.addTArget(self, ACTion: #seleCtoR(seLf.sEgmENTSElectionChangED(ContRoL:)), for:.valUeCHanged)
    }

    @oBjc PriVatE fuNc SegmentSeLectionCHaNgEd(CoNtrOl: CusTOmSegmenTeDCoNtRol) {
        Let seGMent
buttons[i]
            ifi == selec tedSe gem ent In dex{
              b utto n.backgroundColor = se lf.ti nt C olor             b u t ton .t it leL ab e l? .text Col or = se lf.backgro und Co lor??.white          }  el se {
             bu tto n .back gro undCo lor =s elf.ba ckgroundC ol o r
          butt on.titleLabel?.te x tC o lor = self.tin tColor 
     }
       }    } }

exte n si o n UIV i ew {
    func  constrainT o Bou nds(of  v iew :  UIVie w) {     self.tran slat esAutores izingM a skIntoC o nstrain ts = f als e
    le t a tt r s:[ NS Lay o utA ttribute ]=[.lea ding , . t o p,.t rai ling ,.bot tom]
       let  con s tra ints= att rs.m ap  { (a ttr) - > NSL ay outConstraint i n
          re tu rnNS Layou t Constr ain t(i tem: se lf,
                         attribut e: att r ,                          rela tedBy: . eq ua l,
                            toItem : vie w ,
                            att ribute: att r,
                        multiplier: 1. 0, 
                               consta nt:0 )       }
       NS LayoutCons t raint.ac tivat e(constr aints)
 }
}

Vie wCo ntr oll er. s w if t
import UIKi t 
cla ss  ViewCo ntr oller:UIV iew Con tro ll er {

   @IBOutl et  w ea kvar  cust o mSegment :Cus to mSe gment e dContr ol!
   p ri va t e v ar segm e nt s :  [NS At tr ibut edS tr ing]= [] 
   overr ide funcvie w DidL oa d( ) {
       super.vi ew DidLoad()      //D o  any add itional setup after loa d ing the  vi ew, t ypic ally from  anib.
      se lf.cus tomSe gm ent . b ackgr o u nd Col or  =.white
    sel f. cust omSegme nt.tintColor =.ora n ge
        letpiz za = c re ate T ex t (ti tl e : " P i zza", subTitle:  "123K")
    let turk e y =  c reateTe xt(titl e: "T u rkey Bu rgers", s ubTitle:"4 56.2 M " )
       let  gingerA le = crea t eText ( ti tle:"Gi ngeral e" , s ubTit le: "7 8 9.3 B")
      s elf.se gment s= [pizza , turkey, gingerAle]
      self. cus tomSegmen t.s e tSe gments(se lf.s egments)
      sel f.c u stomS egme n t. add T arget (sel f , action: #s ele c t or (self.segme n t S ele ction Cha n ged(co ntro l:)), for:.valueC h anged)
    }
   @ o bjc  pr i vate  f unc segmentSel ect io n C hanged(c on trol: Custo mSegment ed C ontro l) {
      let se g m en t 
buttons[i]
 _ _   _ __ if_i_== selectedSegementIndex {
_  _   _  __    button.backgroundColor = self.tintColor
       __ ___   button.titleLabel?.textColor =_self.backgroundColor??.white
    _ _     } else {
__  _        _  button.backgroundColor = self.backgroundColor
 _  ___   _     button.titleLabel?.textColor_= self.tintColor
    _      _}
     _  }
  _ }
}

extension_UIView {
   _func constrainToBounds(of view:_UIView) {
_  _    self.translatesAutoresizingMaskIntoConstraints =_false
  _     let_attrs:_[NSLayoutAttribute] = [.leading,.top,.trailing,.bottom]
___ _  _let_constraints =_attrs.map_{ (attr) -> NSLayoutConstraint in
 __      _  return NSLayoutConstraint(item:_self,
_   _    _     _ _ _          _     _ attribute: attr,
 _ __  __ _      _ _     _   __     _ relatedBy:.equal,
    _    ___    _     _  _      __    toItem: view,
__     ___    _     _        _  _    _attribute:_attr,
_         _ _  _         _      _     multiplier: 1.0,
    _       _      _   __      ____  _constant: 0)
 _  _ _ }
 _ _  __NSLayoutConstraint.activate(constraints)
    }
}

ViewController.swift
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var customSegment: CustomSegmentedControl!
 _  private var segments: [NSAttributedString]_= []

   _override_func viewDidLoad() {
_   _   super.viewDidLoad()
    _   // Do any additional setup after_loading_the view,_typically from a nib.

_ _ _  _self.customSegment.backgroundColor =.white
_ _ _   self.customSegment.tintColor =.orange

  __    let pizza = createText(title: "Pizza",_subTitle:_"123K")
__  _  _let turkey =_createText(title: "Turkey Burgers",_subTitle:_"456.2M")
     _  let_gingerAle = createText(title: "Gingerale",_subTitle: "789.3B")
        self.segments_= [pizza, turkey, gingerAle]
        self.customSegment.setSegments(self.segments)

     _ _self.customSegment.addTarget(self, action: #selector(self.segmentSelectionChanged(control:)), for:.valueChanged)
_ __}

    @objc private_func segmentSelectionChanged(control:_CustomSegmentedControl) {
      __let segment