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

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

提交回复
热议问题