Skip to content

Commit 4ac1b9d

Browse files
committed
fix: adjust segment position after layout
1 parent 9c6a2a7 commit 4ac1b9d

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

LUNSegmentedControl/LUNSegmentedControl/LUNSegmentedControl/LUNSegmentedControl.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ @interface LUNSegmentedControl() <UIScrollViewDelegate, UIGestureRecognizerDeleg
3232
@property (nonatomic, strong) NSMutableArray <NSLayoutConstraint *> *addedConstraintsToRemove;
3333
@property (nonatomic, assign) BOOL viewWasLayoutSubviews;
3434
@property (nonatomic, assign) BOOL layoutDependentValuesWasUpdated;
35+
@property (nonatomic, assign) BOOL layoutAfterScrolling;
3536

3637
@end
3738

@@ -68,9 +69,10 @@ - (void)layoutSubviews {
6869
[super layoutSubviews];
6970
[self layoutIfNeeded];
7071
self.viewWasLayoutSubviews = YES;
71-
if (!self.layoutDependentValuesWasUpdated) {
72+
if (!self.layoutDependentValuesWasUpdated || self.layoutAfterScrolling) {
7273
[self updateLayoutDependentValues];
7374
}
75+
self.layoutAfterScrolling = YES;
7476
}
7577

7678
#pragma mark - Init defaults
@@ -727,7 +729,10 @@ - (void)reloadData {
727729
}
728730
}
729731
- (void)updateLayoutDependentValues {
732+
int state = self.currentState;
730733
self.currentState = 0;
734+
self.currentState = self.statesCount-1;
735+
self.currentState = state;
731736
self.layoutDependentValuesWasUpdated = YES;
732737
}
733738

@@ -787,6 +792,7 @@ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
787792
self.userInteractionEnabled = YES;
788793
}
789794
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
795+
self.layoutAfterScrolling = NO;
790796
if ([self.delegate respondsToSelector:@selector(segmentedControl:didScrollWithXOffset:)]) {
791797
[self.delegate segmentedControl:self didScrollWithXOffset:self.frame.size.width - self.scrollView.contentOffset.x - self.selectorView.frame.size.width];
792798
}

0 commit comments

Comments
 (0)