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.

111 lines
4.4 KiB

6 years ago
  1. // SegmentedPagerTabStripViewController.swift
  2. // XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip )
  3. //
  4. // Copyright (c) 2017 Xmartlabs ( 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. public struct SegmentedPagerTabStripSettings {
  26. public struct Style {
  27. public var segmentedControlColor: UIColor?
  28. }
  29. public var style = Style()
  30. }
  31. open class SegmentedPagerTabStripViewController: PagerTabStripViewController, PagerTabStripDataSource, PagerTabStripDelegate {
  32. @IBOutlet weak public var segmentedControl: UISegmentedControl!
  33. open var settings = SegmentedPagerTabStripSettings()
  34. public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  35. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  36. pagerBehaviour = PagerTabStripBehaviour.common(skipIntermediateViewControllers: true)
  37. delegate = self
  38. datasource = self
  39. }
  40. required public init?(coder aDecoder: NSCoder) {
  41. super.init(coder: aDecoder)
  42. pagerBehaviour = PagerTabStripBehaviour.common(skipIntermediateViewControllers: true)
  43. delegate = self
  44. datasource = self
  45. }
  46. private(set) var shouldUpdateSegmentedControl = true
  47. open override func viewDidLoad() {
  48. super.viewDidLoad()
  49. let auxSegmentedControl = segmentedControl ?? UISegmentedControl()
  50. segmentedControl = auxSegmentedControl
  51. if segmentedControl.superview == nil {
  52. navigationItem.titleView = segmentedControl
  53. }
  54. segmentedControl.tintColor = settings.style.segmentedControlColor ?? segmentedControl.tintColor
  55. segmentedControl.addTarget(self, action: #selector(SegmentedPagerTabStripViewController.segmentedControlChanged(_:)), for: .valueChanged)
  56. reloadSegmentedControl()
  57. }
  58. open override func reloadPagerTabStripView() {
  59. super.reloadPagerTabStripView()
  60. if isViewLoaded {
  61. reloadSegmentedControl()
  62. }
  63. }
  64. func reloadSegmentedControl() {
  65. segmentedControl.removeAllSegments()
  66. for (index, item) in viewControllers.enumerated() {
  67. let child = item as! IndicatorInfoProvider // swiftlint:disable:this force_cast
  68. if let image = child.indicatorInfo(for: self).image {
  69. segmentedControl.insertSegment(with: image, at: index, animated: false)
  70. } else {
  71. segmentedControl.insertSegment(withTitle: child.indicatorInfo(for: self).title, at: index, animated: false)
  72. }
  73. }
  74. segmentedControl.selectedSegmentIndex = currentIndex
  75. }
  76. @objc func segmentedControlChanged(_ sender: UISegmentedControl) {
  77. let index = sender.selectedSegmentIndex
  78. updateIndicator(for: self, fromIndex: currentIndex, toIndex: index)
  79. shouldUpdateSegmentedControl = false
  80. moveToViewController(at: index)
  81. }
  82. // MARK: - PagerTabStripDelegate
  83. open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int) {
  84. if shouldUpdateSegmentedControl {
  85. segmentedControl.selectedSegmentIndex = toIndex
  86. }
  87. }
  88. // MARK: - UIScrollViewDelegate
  89. open override func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
  90. super.scrollViewDidEndScrollingAnimation(scrollView)
  91. shouldUpdateSegmentedControl = true
  92. }
  93. }