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

前端 未结 29 1429
醉梦人生
醉梦人生 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:41

    Any constraint-based UIView can be a good tableHeaderView.

    One needs to set a tableFooterView before and then impose additional trailing constraint on tableFooterView and tableHeaderView.

    - (void)viewDidLoad {
    
        ........................
        // let self.headerView is some constraint-based UIView
        self.tableView.tableFooterView = [UIView new];
        [self.headerView layoutIfNeeded];
        self.tableView.tableHeaderView = self.headerView;
    
        [self.tableView.leadingAnchor constraintEqualToAnchor:self.headerView.leadingAnchor].active = YES;
        [self.tableView.trailingAnchor constraintEqualToAnchor:self.headerView.trailingAnchor].active = YES;
        [self.tableView.topAnchor constraintEqualToAnchor:self.headerView.topAnchor].active = YES;
        [self.tableFooterView.trailingAnchor constraintEqualToAnchor:self.headerView.trailingAnchor].active = YES;
    

    }

    One can find all details and code snippets here

提交回复
热议问题