使用Masonry动态设置tableview表头---tableHeaderView(解决表头背景色设置不了问题)

本文介绍了一种在iOS应用中实现热门城市选择器的方法。通过动态加载按钮并使用Auto Layout进行布局调整,实现了响应式的界面设计。文章详细展示了如何在表头中添加热门城市的动态控件,并设置按钮的样式及交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 //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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值