Using autolayout in a tableHeaderView

前端 未结 8 2147
谎友^
谎友^ 2020-12-01 06:03

I have a UIView subclass that contains a multi-line UILabel. This view uses autolayout.

8条回答
  •  忘掉有多难
    2020-12-01 06:27

    Your constraints were just a little off. Take a look at this and let me know if you have any questions. For some reason I had difficulty getting the background of the view to stay red? So I created a filler view that fills the gap created by having a titleLabel and subtitleLabel height that is greater than the height of the imageView

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self)
        {
            self.backgroundColor = [UIColor redColor];
    
            self.imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"Exclamation"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
            self.imageView.tintColor = [UIColor whiteColor];
            self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
            self.imageView.backgroundColor = [UIColor redColor];
            [self addSubview:self.imageView];
            [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self);
                make.width.height.equalTo(@40);
                make.top.equalTo(self).offset(0);
            }];
    
            self.titleLabel = [[UILabel alloc] init];
            self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
            self.titleLabel.font = [UIFont systemFontOfSize:14];
            self.titleLabel.textColor = [UIColor whiteColor];
            self.titleLabel.backgroundColor = [UIColor redColor];
            [self addSubview:self.titleLabel];
            [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).offset(0);
                make.left.equalTo(self.imageView.mas_right).offset(0);
                make.right.equalTo(self).offset(-10);
                make.height.equalTo(@15);
            }];
    
            self.subtitleLabel = [[UILabel alloc] init];
            self.subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
            self.subtitleLabel.font = [UIFont systemFontOfSize:13];
            self.subtitleLabel.textColor = [UIColor whiteColor];
            self.subtitleLabel.numberOfLines = 0;
            self.subtitleLabel.backgroundColor = [UIColor redColor];
            [self addSubview:self.subtitleLabel];
            [self.subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom);
                make.left.equalTo(self.imageView.mas_right);
                make.right.equalTo(self).offset(-10);
            }];
    
            UIView *fillerView = [[UIView alloc] init];
            fillerView.backgroundColor = [UIColor redColor];
            [self addSubview:fillerView];
            [fillerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.imageView.mas_bottom);
                make.bottom.equalTo(self.subtitleLabel.mas_bottom);
                make.left.equalTo(self);
                make.right.equalTo(self.subtitleLabel.mas_left);
            }];
        }
    
        return self;
    }
    

提交回复
热议问题