如何增加HMSegmentedControl计数?

时间:2015-09-23 05:23:01

标签: ios

您好我使用HMSegmentedControl进行滑动标签功能。我的代码是这样的。

// Tying up the segmented control to a scroll view
self.segmentedControl4 = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, 260, viewWidth, 50)];
self.segmentedControl4.sectionTitles = @[@"Worldwide", @"Local", @"Headlines",@"Four",@"Five"];
self.segmentedControl4.selectedSegmentIndex = 1;
self.segmentedControl4.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
self.segmentedControl4.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
self.segmentedControl4.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1]};
self.segmentedControl4.selectionIndicatorColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
self.segmentedControl4.selectionStyle = HMSegmentedControlSelectionStyleBox;
self.segmentedControl4.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationUp;
self.segmentedControl4.tag = 3;

__weak typeof(self) weakSelf = self;
[self.segmentedControl4 setIndexChangeBlock:^(NSInteger index) {
    [weakSelf.scrollView scrollRectToVisible:CGRectMake(viewWidth * index, 0, viewWidth, 200) animated:YES];
}];

[self.view addSubview:self.segmentedControl4];

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 310, viewWidth, 210)];
self.scrollView.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.contentSize = CGSizeMake(viewWidth * 5, 200);
self.scrollView.delegate = self;
[self.scrollView scrollRectToVisible:CGRectMake(viewWidth, 0, viewWidth, 200) animated:NO];
[self.view addSubview:self.scrollView];

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, viewWidth, 210)];
[self setApperanceForLabel:label1];
label1.text = @"Worldwide";
[self.scrollView addSubview:label1];

UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth, 0, viewWidth, 210)];
[self setApperanceForLabel:label2];
label2.text = @"Local";
[self.scrollView addSubview:label2];

UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth * 2, 0, viewWidth, 210)];
[self setApperanceForLabel:label3];
label3.text = @"Headlines";
[self.scrollView addSubview:label3];

UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth * 2, 0, viewWidth, 210)];
[self setApperanceForLabel:label4];
label4.text = @"Four";
[self.scrollView addSubview:label4];

UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth * 2, 0, viewWidth, 210)];
[self setApperanceForLabel:label5];
label5.text = @"Five";
[self.scrollView addSubview:label5];

我的问题是它没有显示这些label4和标签5只显示到3.但是所有5个标题都显示在section titles数组中。我该如何解决这些问题。 请帮我。 谢谢This is the github project I used

2 个答案:

答案 0 :(得分:1)

我不了解HMSegmentedControl,但我使用Page Menu作为分段控制器等滚动页面。您可以将视图控制器直接添加到此菜单段中。

我不确定这是对这个问题的完美答案,但它也可能对其他人有所帮助。

答案 1 :(得分:0)

设置self.segmentedControl4.tag = 5;

提供要在segmentedControl中显示的标记号。