Skip to content

Commit

Permalink
fix: indicator install origin error
Browse files Browse the repository at this point in the history
  • Loading branch information
iWECon committed Apr 25, 2023
1 parent bad8768 commit 09b7b8a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Demo/Segmenter/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class ViewController: UIViewController {
let r = UIView()
r.backgroundColor = .blue
segmenter.isShadowShouldShow = false
segmenter.contentInset.bottom = 10
segmenter.contentInset.bottom = 14
segmenter.currentIndex = 1
// set indicator
segmenter.indicator = LineIndicator()
segmenter.segments = [
Expand Down
2 changes: 2 additions & 0 deletions Sources/Segmenter/Indicator/LineIndicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import UIKit

public final class LineIndicator: UIView, Indicator {

public var spacing: CGFloat { 0 }

public override init(frame: CGRect) {
super.init(frame: frame)

Expand Down
17 changes: 14 additions & 3 deletions Sources/Segmenter/Segmenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ public final class Segmenter: UIControl {
}

/// Indicator, indicating the currently active segment
public var indicator: Indicator?
public var indicator: Indicator? {
didSet {
oldValue?.removeFromSuperview()
DispatchQueue.main.async {
self.reloadIndicator()
}
}
}

/// some property(left/right) won’t be join calculate when use `.cetered` or `.evened`
/// default is .init(top: 0, left: 15, bottom: 6, right: 15)
Expand Down Expand Up @@ -422,11 +429,15 @@ public final class Segmenter: UIControl {
}

layoutSubviews()
reloadIndicator()
}

func reloadIndicator() {
guard let indicator, let selectedSegmentView = segmentViews.filter({ $0.isSelected }).first else { return }
indicator.removeFromSuperview()
guard !segmentViews.isEmpty else { return }
let indexRange = 0 ..< segmentViews.count
guard let indicator, indexRange.contains(currentIndex) else { return }

let selectedSegmentView = segmentViews[currentIndex]
self.scrollContainer.addSubview(indicator)
indicator.install(withSementView: selectedSegmentView)
// set origin
Expand Down

0 comments on commit 09b7b8a

Please sign in to comment.