You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
4.6 KiB

5 years ago
  1. //
  2. // ScrollableSegmentedControl.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon James Kim on 23/07/2019.
  6. // Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. protocol ScrollableSegmentedControlDelegate: class {
  10. func selectItemAt(
  11. index : Int,
  12. onScrollUISegmentController scrollUISegmentController: ScrollableSegmentedControl
  13. )
  14. }
  15. @IBDesignable
  16. class ScrollableSegmentedControl: UIScrollView {
  17. private var segmentedControl: UISegmentedControl = UISegmentedControl()
  18. weak var segmentDelegate: ScrollableSegmentedControlDelegate?
  19. @IBInspectable
  20. public var segmentTintColor: UIColor = .black {
  21. didSet {
  22. self.segmentedControl.tintColor = self.segmentTintColor
  23. }
  24. }
  25. @IBInspectable
  26. public var itemWidth: CGFloat = 150 {
  27. didSet {
  28. }
  29. }
  30. public var segmentFont: UIFont = UIFont.systemFont(ofSize: 13) {
  31. didSet {
  32. self.segmentedControl.setTitleTextAttributes(
  33. [NSAttributedString.Key.font: self.segmentFont],
  34. for: UIControl.State()
  35. )
  36. }
  37. }
  38. public var itemsCount: Int = 3
  39. public var segmentheight : CGFloat = 29.0
  40. public var segmentItems = [String]() {
  41. didSet {
  42. self.itemsCount = segmentItems.count
  43. self.createSegment()
  44. }
  45. }
  46. required init(coder aDecoder: NSCoder) {
  47. super.init(coder: aDecoder)!
  48. createSegment()
  49. }
  50. override init(frame: CGRect) {
  51. super.init(frame: frame)
  52. createSegment()
  53. }
  54. init(frame: CGRect , andItems items:[String]) {
  55. super.init(frame: frame)
  56. self.segmentItems = items
  57. self.itemsCount = segmentItems.count
  58. self.createSegment()
  59. }
  60. func createSegment() {
  61. self.segmentedControl.removeFromSuperview()
  62. segmentheight = self.frame.height
  63. let width = CGFloat(self.itemWidth * CGFloat(self.itemsCount))
  64. self.segmentedControl = UISegmentedControl(
  65. frame: CGRect(x: 0 , y: 0, width: width , height: segmentheight)
  66. )
  67. self.addSubview(self.segmentedControl)
  68. self.backgroundColor = .clear
  69. showsHorizontalScrollIndicator = false
  70. showsVerticalScrollIndicator = false
  71. NSLayoutConstraint(
  72. item: self.segmentedControl,
  73. attribute: NSLayoutConstraint.Attribute.top,
  74. relatedBy: NSLayoutConstraint.Relation.equal,
  75. toItem: self,
  76. attribute: NSLayoutConstraint.Attribute.top,
  77. multiplier: 1,
  78. constant: 0
  79. ).isActive = true
  80. NSLayoutConstraint(
  81. item: self.segmentedControl,
  82. attribute: NSLayoutConstraint.Attribute.bottom,
  83. relatedBy: NSLayoutConstraint.Relation.equal,
  84. toItem: self,
  85. attribute: NSLayoutConstraint.Attribute.bottom,
  86. multiplier: 1,
  87. constant: 0
  88. ).isActive = true
  89. NSLayoutConstraint(
  90. item: self.segmentedControl,
  91. attribute: NSLayoutConstraint.Attribute.leading,
  92. relatedBy: NSLayoutConstraint.Relation.equal,
  93. toItem: self,
  94. attribute: NSLayoutConstraint.Attribute.leading,
  95. multiplier: 1,
  96. constant: 0
  97. ).isActive = true
  98. NSLayoutConstraint(
  99. item: self.segmentedControl,
  100. attribute: NSLayoutConstraint.Attribute.trailing,
  101. relatedBy: NSLayoutConstraint.Relation.equal,
  102. toItem: self,
  103. attribute: NSLayoutConstraint.Attribute.trailing,
  104. multiplier: 1,
  105. constant: 0
  106. ).isActive = true
  107. NSLayoutConstraint(
  108. item: self.segmentedControl,
  109. attribute: NSLayoutConstraint.Attribute.top,
  110. relatedBy: NSLayoutConstraint.Relation.equal,
  111. toItem: self,
  112. attribute: NSLayoutConstraint.Attribute.top,
  113. multiplier: 1,
  114. constant: 0
  115. ).isActive = true
  116. let contentHeight = self.frame.height
  117. self.contentSize = CGSize (width: width, height: contentHeight)
  118. self.segmentedControl.setTitleTextAttributes(
  119. [NSAttributedString.Key.font: self.segmentFont],
  120. for: UIControl.State()
  121. )
  122. self.segmentedControl.tintColor = self.segmentTintColor
  123. insertItems()
  124. self.segmentedControl.addTarget(
  125. self,
  126. action: #selector(self.segmentChangeSelectedIndex(_:)),
  127. for: .valueChanged
  128. )
  129. self.segmentedControl.selectedSegmentIndex = 0
  130. }
  131. func insertItems() {
  132. for item in segmentItems {
  133. self.segmentedControl.insertSegment(
  134. withTitle: item,
  135. at: (segmentItems.firstIndex(of: item))!,
  136. animated: false
  137. )
  138. }
  139. }
  140. @objc func segmentChangeSelectedIndex(_ sender: AnyObject) {
  141. segmentDelegate?.selectItemAt(
  142. index: self.segmentedControl.selectedSegmentIndex,
  143. onScrollUISegmentController: self
  144. )
  145. print("\(self.segmentedControl.selectedSegmentIndex)")
  146. }
  147. }