//header
self.headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
self.headerView.backgroundColor = [UIColor clearColor];
self.myTableView.tableHeaderView = self.headerView;
//热门city
UIView *hotTitV = [[UIView alloc]init];
[self.headerView addSubview:hotTitV];
hotTitV.backgroundColor = BKG_GRAY_COLOR;
[hotTitV mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.headerView.mas_top);
make.left.right.mas_equalTo(self.headerView);
make.height.mas_equalTo(36);
}];
UILabel *htL = [[UILabel alloc] init];
htL.textColor = TEXT_LIGHT_GRAY;
htL.font = MIDDLE_LIT_FONT;
htL.text = @"热门城市";
[hotTitV addSubview:htL];
[htL mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(hotTitV.mas_centerY);
make.left.mas_equalTo(hotTitV.mas_left).mas_offset(TEXT_EDGE_DISTANCE);
}];
self.showHotView = [[UIView alloc]init];
[self.showHotView removeFromSuperview];
[self.headerView addSubview:self.showHotView];
self.showHotView.backgroundColor = [UIColor whiteColor];
[self.showHotView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(hotTitV.mas_bottom);
make.left.right.mas_equalTo(hotTitV);
make.height.mas_equalTo(50);
}];
//动态加载控件在表头上
CGFloat btWidth = (SCREEN_WIDTH-5*TEXT_EDGE_DISTANCE)/4.0;
CGFloat btHeight = 30;
for (int i =0; i<self.hotCityArr.count; i++) {
NSDictionary *dict = self.hotCityArr[i];
UIButton *bt = [UIButton buttonWithType:UIButtonTypeSystem];
[self.showHotView addSubview:bt];
[bt setTintColor:TEXT_BLACK_GRAY];
bt.titleLabel.font = MIDDLE_LIT_FONT;
bt.tag = 100+i;
[self.btnArr addObject:bt];
bt.backgroundColor = [UIColor whiteColor];
bt.layer.cornerRadius = 5;
bt.layer.borderColor = BKG_GRAY_COLOR.CGColor;
bt.layer.borderWidth = 0.5;
[bt addTarget:self action:@selector(clickedCity:) forControlEvents:UIControlEventTouchUpInside];
[bt setTitle:[dict objectForKey:@"city"] forState:UIControlStateNormal];
[bt mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.showHotView.mas_top).mas_offset(TEXT_LINE_DISTANCE*(i/4+1)+btHeight*(i/4));
make.left.mas_equalTo(self.showHotView.mas_left).mas_offset(TEXT_EDGE_DISTANCE*((i%4)+1)+btWidth*(i%4));
make.size.mas_equalTo(CGSizeMake(btWidth, btHeight));
}];
[self.showHotView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(hotTitV.mas_bottom);
make.left.right.mas_equalTo(hotTitV);
make.bottom.mas_equalTo(bt.mas_bottom).mas_offset(TEXT_LINE_DISTANCE);
}];
////下面注释解绑后视图样式
[self.showHotView layoutIfNeeded];
}
//****注意headerView 一定要用frame不能使用 mas ,如果使用MAS一定要再加一层然后再用frame,这样tableHeaderView就会有背景色了
self.headerView.frame = CGRectMake(0, 0, self.showHotView.frame.size.width, self.showHotView.frame.size.height+36);
[self.headerView layoutIfNeeded];
NSLog(@"height %lf",self.headerView.bounds.size.height);