From 3ed40e1b55e137998f91fbb110f267d347445de6 Mon Sep 17 00:00:00 2001 From: Raxit Majithiya Date: Mon, 15 May 2023 09:56:39 -0400 Subject: [PATCH 1/2] Add more configs: - Alignment mode for the title layer. - Content insets for the scrollView. - Option to disable scrolling to the selected segment on user selection. --- HMSegmentedControl/HMSegmentedControl.h | 17 +++++++++++++++++ HMSegmentedControl/HMSegmentedControl.m | 16 ++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/HMSegmentedControl/HMSegmentedControl.h b/HMSegmentedControl/HMSegmentedControl.h index ab8a34ec..f9c0634c 100755 --- a/HMSegmentedControl/HMSegmentedControl.h +++ b/HMSegmentedControl/HMSegmentedControl.h @@ -85,6 +85,11 @@ typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition) { */ @property (nonatomic, strong) NSDictionary *titleTextAttributes UI_APPEARANCE_SELECTOR; +/** + Alignment mode for the title layer. + Defaults to Center. + */ +@property (nonatomic, strong) CATextLayerAlignmentMode titleLayerAlignmentMode; /* Text attributes to apply to selected item title text. @@ -226,6 +231,12 @@ typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition) { */ @property (nonatomic, readwrite) CGFloat selectionIndicatorHeight; +/** + Insets for scrollView + Default is .zero + */ +@property (nonatomic, readwrite) UIEdgeInsets scrollViewInsets; + /** Edge insets for the selection indicator. NOTE: This does not affect the bounding box of HMSegmentedControlSelectionStyleBox @@ -255,6 +266,12 @@ typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition) { */ @property (nonatomic) BOOL shouldAnimateUserSelection; +/** + Default is YES. Set to NO to disable scrolling to the selected segment on user selection. + Use shouldAnimateUserSelection to disable the animation on indicator. + */ +@property (nonatomic) BOOL shouldScrollToUserSelectionAnimated; + - (instancetype)initWithSectionTitles:(NSArray *)sectiontitles; - (instancetype)initWithSectionImages:(NSArray *)sectionImages sectionSelectedImages:(NSArray *)sectionSelectedImages; - (instancetype)initWithSectionImages:(NSArray *)sectionImages sectionSelectedImages:(NSArray *)sectionSelectedImages titlesForSections:(NSArray *)sectiontitles; diff --git a/HMSegmentedControl/HMSegmentedControl.m b/HMSegmentedControl/HMSegmentedControl.m index 51e26e4e..7d4d041f 100755 --- a/HMSegmentedControl/HMSegmentedControl.m +++ b/HMSegmentedControl/HMSegmentedControl.m @@ -145,6 +145,7 @@ - (void)commonInit { self.scrollView.scrollsToTop = NO; self.scrollView.showsVerticalScrollIndicator = NO; self.scrollView.showsHorizontalScrollIndicator = NO; + self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; [self addSubview:self.scrollView]; _backgroundColor = [UIColor whiteColor]; @@ -154,6 +155,7 @@ - (void)commonInit { self.selectedSegmentIndex = 0; self.segmentEdgeInset = UIEdgeInsetsMake(0, 5, 0, 5); + self.scrollViewInsets = UIEdgeInsetsZero; self.selectionIndicatorHeight = 5.0f; self.selectionIndicatorEdgeInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f); self.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe; @@ -167,8 +169,10 @@ - (void)commonInit { _verticalDividerColor = [UIColor blackColor]; self.borderColor = [UIColor blackColor]; self.borderWidth = 1.0f; + self.titleLayerAlignmentMode = kCAAlignmentCenter; self.shouldAnimateUserSelection = YES; + self.shouldScrollToUserSelectionAnimated = YES; self.selectionIndicatorArrowLayer = [CALayer layer]; self.selectionIndicatorStripLayer = [CALayer layer]; @@ -357,7 +361,7 @@ - (void)drawRect:(CGRect)rect { CATextLayer *titleLayer = [CATextLayer layer]; titleLayer.frame = rect; - titleLayer.alignmentMode = kCAAlignmentCenter; + titleLayer.alignmentMode = self.titleLayerAlignmentMode; if ([UIDevice currentDevice].systemVersion.floatValue < 10.0 ) { titleLayer.truncationMode = kCATruncationEnd; } @@ -778,7 +782,7 @@ - (CGRect)frameForFillerSelectionIndicator { } - (void)updateSegmentsRects { - self.scrollView.contentInset = UIEdgeInsetsZero; + self.scrollView.contentInset = self.scrollViewInsets; self.scrollView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)); if ([self sectionCount] > 0) { @@ -937,8 +941,8 @@ - (CGFloat)totalSegmentedControlWidth { } } -- (void)scrollToSelectedSegmentIndex:(BOOL)animated { - [self scrollTo:self.selectedSegmentIndex animated:animated]; +- (void)scrollToSelectedSegmentIndex { + [self scrollTo:self.selectedSegmentIndex animated:self.shouldScrollToUserSelectionAnimated]; } - (void)scrollTo:(NSUInteger)index animated:(BOOL)animated { @@ -995,8 +999,8 @@ - (void)setSelectedSegmentIndex:(NSUInteger)index animated:(BOOL)animated notify [self.selectionIndicatorStripLayer removeFromSuperlayer]; [self.selectionIndicatorBoxLayer removeFromSuperlayer]; } else { - [self scrollToSelectedSegmentIndex:animated]; - + [self scrollToSelectedSegmentIndex]; + if (animated) { // If the selected segment layer is not added to the super layer, that means no // index is currently selected, so add the layer then move it to the new From d86183526bd158572a18264abc0c911c31212bf2 Mon Sep 17 00:00:00 2001 From: Raxit Majithiya Date: Fri, 8 Dec 2023 17:41:52 -0500 Subject: [PATCH 2/2] Disable usage of scrollsToTop in tvOS --- HMSegmentedControl/HMSegmentedControl.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HMSegmentedControl/HMSegmentedControl.m b/HMSegmentedControl/HMSegmentedControl.m index 7d4d041f..21c015a0 100755 --- a/HMSegmentedControl/HMSegmentedControl.m +++ b/HMSegmentedControl/HMSegmentedControl.m @@ -142,7 +142,10 @@ - (void)awakeFromNib { - (void)commonInit { self.scrollView = [[HMScrollView alloc] init]; self.scrollView.delegate = self; +#ifdef TARGET_OS_TV +#else self.scrollView.scrollsToTop = NO; +#endif self.scrollView.showsVerticalScrollIndicator = NO; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;