Is it possible to use AutoLayout with UITableView's tableHeaderView?

前端 未结 29 1408
醉梦人生
醉梦人生 2020-11-28 19:51

Since I discovered AutoLayout I use it everywhere, now I\'m trying to use it with a tableHeaderView.

I made a subclass of

29条回答
  •  悲&欢浪女
    2020-11-28 20:40

    Share my approach.

    UITableView+XXXAdditions.m

    - (void)xxx_setTableHeaderView:(UIView *)tableHeaderView layoutBlock:(void(^)(__kindof UIView *tableHeaderView, CGFloat *containerViewHeight))layoutBlock {
          CGFloat containerViewHeight = 0;
          UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
          [backgroundView addSubview:tableHeaderView];
          layoutBlock(tableHeaderView, &containerViewHeight);
    
          backgroundView.frame = CGRectMake(0, 0, 0, containerViewHeight);
    
          self.tableHeaderView = backgroundView;
    }
    

    Usage.

    [self.tableView xxx_setTableHeaderView:myView layoutBlock:^(__kindof UIView * _Nonnull tableHeaderView, CGFloat *containerViewHeight) {
        *containerViewHeight = 170;
    
        [tableHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
          make.top.equalTo(@20);
          make.centerX.equalTo(@0);
          make.size.mas_equalTo(CGSizeMake(130, 130));
        }];
      }];
    

提交回复
热议问题