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

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

    Tips: If you use method setAndLayoutTableHeaderView, you should update subviews's frame,so in this situation UILabel's preferredMaxLayoutWidth should call before systemLayoutSizeFittingSize called, do not call in layoutSubview.

    code show

    0 讨论(0)
  • 2020-11-28 20:21

    My table header view is a UIView subclass - I created a single contentView UIView within the initializer, with its bounds the same as the table header view's frame and added all my objects as a subview of that.

    Then add the constraints for your objects within the table header view's layoutSubviews method rather than within the initializer. That solved the crash.

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:CGRectMake(0, 0, 0, 44.0)];
        if (self) {
            UIView *contentView = [[UIView alloc] initWithFrame:self.bounds];
            contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
            // add other objects as subviews of content view
    
        }
        return self;
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        // remake constraints here
    }
    
    0 讨论(0)
  • 2020-11-28 20:21

    My AutoLayout is working very good:

    CGSize headerSize = [headerView systemLayoutSizeFittingSize:CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds), 0) withHorizontalFittingPriority:UILayoutPriorityRequired verticalFittingPriority:UILayoutPriorityFittingSizeLevel];
    headerView.frame = CGRectMake(0, 0, headerSize.width, headerSize.height);
    self.tableView.tableHeaderView = headerView;
    
    0 讨论(0)
  • 2020-11-28 20:27

    I saw a lot of methods here doing so much unnecessary stuff, but you don't need that much to use auto layout in the header view. You just have to create you xib file, put your constraints and instantiate it like this:

    func loadHeaderView () {
            guard let headerView = Bundle.main.loadNibNamed("CourseSearchHeader", owner: self, options: nil)?[0] as? UIView else {
                return
            }
            headerView.autoresizingMask = .flexibleWidth
            headerView.translatesAutoresizingMaskIntoConstraints = true
            tableView.tableHeaderView = headerView
        }
    
    0 讨论(0)
  • 2020-11-28 20:28

    The following worked for me.

    1. Use a plain old UIView as the header view.
    2. Add subviews to that UIView
    3. Use autolayout on the subviews

    The main benefit I see is limiting frame calculations. Apple should really update UITableView's API to make this easier.

    Example using SnapKit:

    let layoutView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 60))
    layoutView.backgroundColor = tableView.backgroundColor
    tableView.tableHeaderView = layoutView
    
    let label = UILabel()
    layoutView.addSubview(label)
    label.text = "I'm the view you really care about"
    label.snp_makeConstraints { make in
        make.edges.equalTo(EdgeInsets(top: 10, left: 15, bottom: -5, right: -15))
    }
    
    0 讨论(0)
  • 2020-11-28 20:28

    For Xamarin users:

    public override void ViewDidLayoutSubviews()
    {
        base.ViewDidLayoutSubviews();
    
        TableviewHeader.SetNeedsLayout();
        TableviewHeader.LayoutIfNeeded();
    
        var height = TableviewHeader.SystemLayoutSizeFittingSize(UIView.UILayoutFittingCompressedSize).Height;
        var frame = TableviewHeader.Frame;
        frame.Height = height;
        TableviewHeader.Frame = frame;
    }
    

    Assuming you named the header view of your tableview as TableviewHeader

    0 讨论(0)
提交回复
热议问题