- (void)viewDidLoad {
[super viewDidLoad];
[self setChildVC];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"orangeBackground"] forBarMetrics:UIBarMetricsDefault];
self.view.backgroundColor = [UIColor whiteColor];
[self navigationItemTitleViewLabel];
[self setBqScollView];
[self setTitelView];
}
#pragma mark - 设置标题栏
- (void)setTitelView{
//标签
UIView *titleView = [[UIView alloc] init];
titleView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1];
titleView.height = 35;
titleView.width = self.view.width;
[self.view addSubview:titleView];
self.titleView = titleView;
UIView *zsView = [[UIView alloc] init];
zsView.height = 2;
zsView.y = titleView.height - zsView.height;
zsView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"orangeBackground"]];
self.zsView = zsView;
//创建内部按钮
NSArray *titleArr = @[@"类别",@"功效",@"人群",@"营养"];
CGFloat buttonW = self.view.width / titleArr.count;
CGFloat buttoH = titleView.height;
CGFloat buttonY = 0;
for (NSInteger i = 0; i < titleArr.count; i++) {
UIButton *button = [[UIButton alloc] init];
button.tag = i;
button.width = buttonW;
button.height = buttoH;
button.y = buttonY;
button.x = i * buttonW;
[button setTitle:titleArr[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"orangeBackground"]] forState:UIControlStateHighlighted];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:button];
if (i == 0) {
button.enabled = NO;
self.selectButton = button;
// 让按钮内部的label根据文字来计算尺寸(如果没有这一句,按钮titleLabel的宽度为0)
[button.titleLabel sizeToFit];
//强制布局
[button.titleLabel layoutIfNeeded];
self.zsView.width = button.titleLabel.width;
self.zsView.centerX = button.centerX;
}
}
[titleView addSubview:zsView];
}
- (void)titleButtonClick:(UIButton *)titleButton{
//设置选中按钮的状态
self.selectButton.enabled = YES;
titleButton.enabled = NO;
self.selectButton = titleButton;
//设置选中按钮后指示条的位置
[UIView animateWithDuration:0.1 animations:^{
self.zsView.width = titleButton.titleLabel.width;
self.zsView.centerX = titleButton.centerX;
}];
//滚动
CGPoint offset = self.bqscview.contentOffset;
offset.x = self.view.width * titleButton.tag;
[self.bqscview setContentOffset:offset animated:YES];
}
#pragma mark - 添加ScollView
- (void)setBqScollView{
self.automaticallyAdjustsScrollViewInsets = NO;
UIScrollView *bqScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 2, SCREEN_W, SCREEN_H - 54)];
bqScrollView.bounces = NO;
bqScrollView.delegate = self;
bqScrollView.pagingEnabled = YES;
[self.view addSubview:bqScrollView];
bqScrollView.contentSize = CGSizeMake(self.childViewControllers.count * bqScrollView.width, 0);
[self.view insertSubview:bqScrollView atIndex:0];
[self.view addSubview:bqScrollView];
self.bqscview = bqScrollView;
//将第一个控制器的view添加进去
[self scrollViewDidEndScrollingAnimation:bqScrollView];
}
#pragma mark - 添加子控制器
- (void)setChildVC{
LBVC *lbVC = [[LBVC alloc] initWithStyle:UITableViewStylePlain];
[self addChildViewController:lbVC];
GXVC *gxVC = [[GXVC alloc] initWithStyle:UITableViewStylePlain];
[self addChildViewController:gxVC];
RQVC *rqVC = [[RQVC alloc] initWithStyle:UITableViewStylePlain];
[self addChildViewController:rqVC];
YYVC *yyVC = [[YYVC alloc] initWithStyle:UITableViewStylePlain];
[self addChildViewController:yyVC];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
//取出索引
NSInteger index = scrollView.contentOffset.x / scrollView.width;
//取出子控制器
UITableViewController *childVC = self.childViewControllers[index];
childVC.view.y = -10;
childVC.view.x = scrollView.contentOffset.x;
childVC.view.height = scrollView.height;
childVC.tableView.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.height, 0, self.tabBarController.tabBar.height, 0);
childVC.tableView.scrollIndicatorInsets = childVC.tableView.contentInset;
[scrollView addSubview:childVC.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self scrollViewDidEndScrollingAnimation:scrollView];
//点击titleButton
NSInteger index = (scrollView.contentOffset.x / scrollView.width);
[self titleButtonClick:self.titleView.subviews[index]];
}
- (void)navigationItemTitleViewLabel{
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 33)];
titleLabel.text = @"食材百科";
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
self.navigationItem.titleView = titleLabel;
}