// // ScrollableSegmentedControl.swift // GME Remit // // Created by InKwon James Kim on 23/07/2019. // Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit protocol ScrollableSegmentedControlDelegate: class { func selectItemAt( index : Int, onScrollUISegmentController scrollUISegmentController: ScrollableSegmentedControl ) } @IBDesignable class ScrollableSegmentedControl: UIScrollView { private var segmentedControl: UISegmentedControl = UISegmentedControl() weak var segmentDelegate: ScrollableSegmentedControlDelegate? @IBInspectable public var segmentTintColor: UIColor = .black { didSet { self.segmentedControl.tintColor = self.segmentTintColor } } @IBInspectable public var itemWidth: CGFloat = 150 { didSet { } } public var segmentFont: UIFont = UIFont.systemFont(ofSize: 13) { didSet { self.segmentedControl.setTitleTextAttributes( [NSAttributedString.Key.font: self.segmentFont], for: UIControl.State() ) } } public var itemsCount: Int = 3 public var segmentheight : CGFloat = 29.0 public var segmentItems = [String]() { didSet { self.itemsCount = segmentItems.count self.createSegment() } } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! createSegment() } override init(frame: CGRect) { super.init(frame: frame) createSegment() } init(frame: CGRect , andItems items:[String]) { super.init(frame: frame) self.segmentItems = items self.itemsCount = segmentItems.count self.createSegment() } func createSegment() { self.segmentedControl.removeFromSuperview() segmentheight = self.frame.height let width = CGFloat(self.itemWidth * CGFloat(self.itemsCount)) self.segmentedControl = UISegmentedControl( frame: CGRect(x: 0 , y: 0, width: width , height: segmentheight) ) self.addSubview(self.segmentedControl) self.backgroundColor = .clear showsHorizontalScrollIndicator = false showsVerticalScrollIndicator = false NSLayoutConstraint( item: self.segmentedControl, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0 ).isActive = true NSLayoutConstraint( item: self.segmentedControl, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0 ).isActive = true NSLayoutConstraint( item: self.segmentedControl, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0 ).isActive = true NSLayoutConstraint( item: self.segmentedControl, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0 ).isActive = true NSLayoutConstraint( item: self.segmentedControl, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0 ).isActive = true let contentHeight = self.frame.height self.contentSize = CGSize (width: width, height: contentHeight) self.segmentedControl.setTitleTextAttributes( [NSAttributedString.Key.font: self.segmentFont], for: UIControl.State() ) self.segmentedControl.tintColor = self.segmentTintColor insertItems() self.segmentedControl.addTarget( self, action: #selector(self.segmentChangeSelectedIndex(_:)), for: .valueChanged ) self.segmentedControl.selectedSegmentIndex = 0 } func insertItems() { for item in segmentItems { self.segmentedControl.insertSegment( withTitle: item, at: (segmentItems.firstIndex(of: item))!, animated: false ) } } @objc func segmentChangeSelectedIndex(_ sender: AnyObject) { segmentDelegate?.selectItemAt( index: self.segmentedControl.selectedSegmentIndex, onScrollUISegmentController: self ) print("\(self.segmentedControl.selectedSegmentIndex)") } }