闲暇时自定义封装的用于页面顶部的分段控制器
用法:
设置全局属性@property (nonatomic, strong)UISegmentedControl *control;
在viewDidLoad中设置control默认选择0:
_control.selectedSegmentIndex = 0;
具体代码:
//设置滚动scrollView
- (void)setScrollView {
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-108)];
_scrollView.delegate = self;
_scrollView.backgroundColor = [UIColor orangeColor];
_scrollView.bounces = NO;
_scrollView.contentSize = CGSizeMake(KScreenWidth*3, 0);
self.tableView1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-108)];
_tableView1.delegate = self;
_tableView1.dataSource = self;
[_tableView1 registerNib:[UINib nibWithNibName:@"Eight1TableViewCell" bundle:nil] forCellReuseIdentifier:@"Eight1TableViewCell"];
self.tableView2 = [[UITableView alloc]initWithFrame:CGRectMake(KScreenWidth, 0, KScreenWidth, KScreenHeight-108)];
_tableView2.delegate = self;
_tableView2.dataSource = self;
[_tableView2 registerNib:[UINib nibWithNibName:@"Eight3ViewCell" bundle:nil] forCellReuseIdentifier:@"Eight3ViewCell"];
self.tableView3 = [[UITableView alloc]initWithFrame:CGRectMake(KScreenWidth*2, 0, KScreenWidth, KScreenHeight-108)];
_tableView3.delegate = self;
_tableView3.dataSource = self;
[_tableView3 registerNib:[UINib nibWithNibName:@"Eight3ViewCell" bundle:nil] forCellReuseIdentifier:@"Eight3ViewCell"];
[self.scrollView addSubview:self.tableView1];
[self.scrollView addSubview:self.tableView2];
[self.scrollView addSubview:self.tableView3];
[self.view addSubview:self.scrollView];
}
//设置Control的位置内容UI
- (void)setSegmentView {
self.control = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"贡献查询",@"奖金明细",@"交易明细", nil]];
UIImage *dividerImage = [UIImage imageNamed:@"chengxian"];
UIImage *segmentSelected = [UIImage imageNamed:@"segcontrol_sel.png"];
UIImage *segmentUnSelected = [UIImage imageNamed:@"segcontrol_Unsel.png"];
[self.control setDividerImage:dividerImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.control setBackgroundImage:segmentSelected forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.control setBackgroundImage:segmentUnSelected forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//修改字体的默认颜色与选中颜色
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIFont boldSystemFontOfSize:12.0f],UITextAttributeFont ,[UIColor whiteColor],UITextAttributeTextColor ,nil];
NSDictionary *dics = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIFont boldSystemFontOfSize:12.0f],UITextAttributeFont ,[UIColor whiteColor],UITextAttributeTextColor ,nil];
[self.control setTitleTextAttributes:dic forState:UIControlStateNormal];
[self.control setTitleTextAttributes:dics forState:UIControlStateSelected];
[_control addTarget:self action:@selector(layoutManager:) forControlEvents:(UIControlEventValueChanged)];
_control.frame = CGRectMake(0, 0, 305, 35);
// [_control setTintColor:[UIColor whiteColor]];
[self.navigationItem setTitleView:_control];
}
选中方法:
-(void)layoutManager:(UISegmentedControl *)sender {
switch (sender.selectedSegmentIndex){
case 0: [_scrollView setContentOffset:CGPointZero];
break;
case 1: [_scrollView setContentOffset:CGPointMake(KScreenWidth, 0)]; break;
case 2: [_scrollView setContentOffset:CGPointMake(KScreenWidth*2, 0)]; default:
break;
}}
滚动代理方法:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { self.control.selectedSegmentIndex = _scrollView.contentOffset.x /KScreenWidth; if (scrollView == _scrollView) {
if (scrollView.contentOffset.x >KScreenWidth/2 &&scrollView.contentOffset.xKScreenWidth*3 /2){
[self.scrollView setContentOffset:CGPointMake(KScreenWidth*2, 0) animated:YES];
}else {
[self.scrollView setContentOffset:CGPointZero animated:YES];
}
}
}