如何在不手动滚动的情况下将 HMSegmentedControl 水平滚动到所选索引
Posted
技术标签:
【中文标题】如何在不手动滚动的情况下将 HMSegmentedControl 水平滚动到所选索引【英文标题】:How to scroll HMSegmentedControl horizontally to the selected index without manually scrolling 【发布时间】:2017-05-15 08:08:04 【问题描述】:我正在使用HMSegmentedControl
6 个部分标题(周一、周二、周三、周四、周五、周六)。
当今天是星期五时,我想将 HMSegmentedControl
滚动到星期五的索引(即索引 4)。
我已将HMSegmentedControl.selectedSegmentIndex
设置为星期五的索引,但它没有滚动到该索引。
当我手动点击星期五然后HMSegmentedControl
滚动。
有谁知道我如何做到这一点。
segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[NSLocalizedString(@"MONDAY", nil), NSLocalizedString(@"TUESDAY", nil), NSLocalizedString(@"WEDNESDAY", nil), NSLocalizedString(@"THURSDAY", nil), NSLocalizedString(@"FRIDAY", nil), NSLocalizedString(@"SATURDAY", nil)]];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
segmentedControl.frame = CGRectMake(0, 44, viewWidth, 45);
segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10);
segmentedControl.backgroundColor = [Nav_Bar_Color toColor];
segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
segmentedControl.selectionIndicatorColor = [UIColor whiteColor];
segmentedControl.verticalDividerEnabled = YES;
segmentedControl.verticalDividerColor = [UIColor clearColor];
segmentedControl.verticalDividerWidth = 1.5f;
[segmentedControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected)
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont fontWithName:FONT_NAME size:FONT_SIZE]];
return attString;
];
[segmentedControl addTarget:self action:@selector(loadTimeTableData:) forControlEvents:UIControlEventValueChanged];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:[NSDate date]];
int weekdayNumber = [components weekday];
weekdayNumber = weekdayNumber - 1;
switch (weekdayNumber)
case 1:
case 7:
segmentedControl.selectedSegmentIndex = 0;
break;
case 2:
segmentedControl.selectedSegmentIndex = 1;
break;
case 3:
segmentedControl.selectedSegmentIndex = 2;
break;
case 4:
segmentedControl.selectedSegmentIndex = 3;
break;
case 5:
segmentedControl.selectedSegmentIndex = 4;
break;
case 6:
segmentedControl.selectedSegmentIndex = 5;
break;
default:
break;
[segmentedControl setSelectedSegmentIndex:segmentedControl.selectedSegmentIndex animated:YES];
[self.view addSubview:segmentedControl];
【问题讨论】:
分享那个的代码。 segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[NSLocalizedString(@"MONDAY", nil), NSLocalizedString(@"TUESDAY", nil), NSLocalizedString(@"WEDNESDAY", nil), NSLocalizedString (@"THURSDAY", nil), NSLocalizedString(@"FRIDAY", nil), NSLocalizedString(@"SATURDAY", nil)]]; segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; CGFloat viewWidth = CGRectGetWidth(self.view.frame); segmentedControl.frame = CGRectMake(0, 44, viewWidth, 45); segmentedControl.selectedSegmentIndex = 4; 你需要调用这个方法 - (void)setSelectedSegmentIndex:(NSUInteger)index animated:(BOOL)animated ..... 例如.. [segmentedControl setSelectedSegmentIndex:4 animated:YES]; @Zck 我已经调用了 setSelectedSegmentIndex 方法,但仍然 segmentedControl 没有滚动到该索引 你的 viewcontroller 中是否添加了任何 scrollView ? 【参考方案1】:您应该在loadTimeTableData:
method 中调用segementControl selected at index
method。
【讨论】:
以上是关于如何在不手动滚动的情况下将 HMSegmentedControl 水平滚动到所选索引的主要内容,如果未能解决你的问题,请参考以下文章
如何在不破坏滚动功能的情况下将 UIImage 添加到 UIScrollView?
如何在不滚动的情况下将项目添加到 ListView 的开头?
在不中断用户滚动的情况下将单元格添加到 UITableView 的顶部