SegmentedRow.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SegmentedRow.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs SRL ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. import UIKit
  26. // MARK: SegmentedCell
  27. open class SegmentedCell<T: Equatable> : Cell<T>, CellType {
  28. @IBOutlet public weak var segmentedControl: UISegmentedControl!
  29. @IBOutlet public weak var titleLabel: UILabel?
  30. private var dynamicConstraints = [NSLayoutConstraint]()
  31. fileprivate var observingTitleText = false
  32. private var awakeFromNibCalled = false
  33. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  34. super.init(style: style, reuseIdentifier: reuseIdentifier)
  35. let segmentedControl = UISegmentedControl()
  36. segmentedControl.translatesAutoresizingMaskIntoConstraints = false
  37. segmentedControl.setContentHuggingPriority(UILayoutPriority(rawValue: 250), for: .horizontal)
  38. self.segmentedControl = segmentedControl
  39. self.titleLabel = self.textLabel
  40. self.titleLabel?.translatesAutoresizingMaskIntoConstraints = false
  41. self.titleLabel?.setContentHuggingPriority(UILayoutPriority(rawValue: 500), for: .horizontal)
  42. self.titleLabel?.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  43. NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: nil) { [weak self] _ in
  44. guard let me = self else { return }
  45. guard me.observingTitleText else { return }
  46. me.titleLabel?.removeObserver(me, forKeyPath: "text")
  47. me.observingTitleText = false
  48. }
  49. NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { [weak self] _ in
  50. guard let me = self else { return }
  51. guard !me.observingTitleText else { return }
  52. me.titleLabel?.addObserver(me, forKeyPath: "text", options: [.new, .old], context: nil)
  53. me.observingTitleText = true
  54. }
  55. NotificationCenter.default.addObserver(forName: UIContentSizeCategory.didChangeNotification, object: nil, queue: nil) { [weak self] _ in
  56. self?.titleLabel = self?.textLabel
  57. self?.setNeedsUpdateConstraints()
  58. }
  59. contentView.addSubview(titleLabel!)
  60. contentView.addSubview(segmentedControl)
  61. titleLabel?.addObserver(self, forKeyPath: "text", options: [.old, .new], context: nil)
  62. observingTitleText = true
  63. imageView?.addObserver(self, forKeyPath: "image", options: [.old, .new], context: nil)
  64. contentView.addConstraint(NSLayoutConstraint(item: segmentedControl, attribute: .centerY, relatedBy: .equal, toItem: contentView, attribute: .centerY, multiplier: 1, constant: 0))
  65. }
  66. required public init?(coder aDecoder: NSCoder) {
  67. super.init(coder: aDecoder)
  68. }
  69. open override func awakeFromNib() {
  70. super.awakeFromNib()
  71. awakeFromNibCalled = true
  72. }
  73. deinit {
  74. segmentedControl.removeTarget(self, action: nil, for: .allEvents)
  75. if !awakeFromNibCalled {
  76. if observingTitleText {
  77. titleLabel?.removeObserver(self, forKeyPath: "text")
  78. }
  79. imageView?.removeObserver(self, forKeyPath: "image")
  80. NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
  81. NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
  82. NotificationCenter.default.removeObserver(self, name: UIContentSizeCategory.didChangeNotification, object: nil)
  83. }
  84. }
  85. open override func setup() {
  86. super.setup()
  87. selectionStyle = .none
  88. segmentedControl.addTarget(self, action: #selector(SegmentedCell.valueChanged), for: .valueChanged)
  89. }
  90. open override func update() {
  91. super.update()
  92. detailTextLabel?.text = nil
  93. updateSegmentedControl()
  94. segmentedControl.selectedSegmentIndex = selectedIndex() ?? UISegmentedControl.noSegment
  95. segmentedControl.isEnabled = !row.isDisabled
  96. }
  97. @objc func valueChanged() {
  98. row.value = (row as! SegmentedRow<T>).options?[segmentedControl.selectedSegmentIndex]
  99. }
  100. open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  101. let obj = object as AnyObject?
  102. if let changeType = change, let _ = keyPath, ((obj === titleLabel && keyPath == "text") || (obj === imageView && keyPath == "image")) &&
  103. (changeType[NSKeyValueChangeKey.kindKey] as? NSNumber)?.uintValue == NSKeyValueChange.setting.rawValue, !awakeFromNibCalled {
  104. setNeedsUpdateConstraints()
  105. updateConstraintsIfNeeded()
  106. }
  107. }
  108. func updateSegmentedControl() {
  109. segmentedControl.removeAllSegments()
  110. (row as! SegmentedRow<T>).options?.reversed().forEach {
  111. if let image = $0 as? UIImage {
  112. segmentedControl.insertSegment(with: image, at: 0, animated: false)
  113. } else {
  114. segmentedControl.insertSegment(withTitle: row.displayValueFor?($0) ?? "", at: 0, animated: false)
  115. }
  116. }
  117. }
  118. open override func updateConstraints() {
  119. guard !awakeFromNibCalled else {
  120. super.updateConstraints()
  121. return
  122. }
  123. contentView.removeConstraints(dynamicConstraints)
  124. dynamicConstraints = []
  125. var views: [String: AnyObject] = ["segmentedControl": segmentedControl]
  126. var hasImageView = false
  127. var hasTitleLabel = false
  128. if let imageView = imageView, let _ = imageView.image {
  129. views["imageView"] = imageView
  130. hasImageView = true
  131. }
  132. if let titleLabel = titleLabel, let text = titleLabel.text, !text.isEmpty {
  133. views["titleLabel"] = titleLabel
  134. hasTitleLabel = true
  135. dynamicConstraints.append(NSLayoutConstraint(item: titleLabel, attribute: .centerY, relatedBy: .equal, toItem: contentView, attribute: .centerY, multiplier: 1, constant: 0))
  136. }
  137. dynamicConstraints.append(NSLayoutConstraint(item: segmentedControl!, attribute: .width, relatedBy: .greaterThanOrEqual, toItem: contentView, attribute: .width, multiplier: 0.3, constant: 0.0))
  138. if hasImageView && hasTitleLabel {
  139. dynamicConstraints += NSLayoutConstraint.constraints(withVisualFormat: "H:[imageView]-(15)-[titleLabel]-[segmentedControl]-|", options: [], metrics: nil, views: views)
  140. } else if hasImageView && !hasTitleLabel {
  141. dynamicConstraints += NSLayoutConstraint.constraints(withVisualFormat: "H:[imageView]-[segmentedControl]-|", options: [], metrics: nil, views: views)
  142. } else if !hasImageView && hasTitleLabel {
  143. dynamicConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-[titleLabel]-[segmentedControl]-|", options: .alignAllCenterY, metrics: nil, views: views)
  144. } else {
  145. dynamicConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-[segmentedControl]-|", options: .alignAllCenterY, metrics: nil, views: views)
  146. }
  147. contentView.addConstraints(dynamicConstraints)
  148. super.updateConstraints()
  149. }
  150. func selectedIndex() -> Int? {
  151. guard let value = row.value else { return nil }
  152. return (row as! SegmentedRow<T>).options?.firstIndex(of: value)
  153. }
  154. }
  155. // MARK: SegmentedRow
  156. /// An options row where the user can select an option from an UISegmentedControl
  157. public final class SegmentedRow<T: Equatable>: OptionsRow<SegmentedCell<T>>, RowType {
  158. required public init(tag: String?) {
  159. super.init(tag: tag)
  160. }
  161. }