如何在Xcode中以编程方式正确使用自动布局

时间:2015-03-12 21:31:23

标签: ios objective-c xcode

我试图将UILabels和图像视图放在自定义表格视图单元格

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
    _Header = [[UILabel alloc]initWithFrame:CGRectMake(94, 10, 206, 21)];
    _Subtitle =[[UILabel alloc]initWithFrame:CGRectMake(94, 30, 206, 21)];
    _CommentLabel =[[UILabel alloc]initWithFrame:CGRectMake(200, 71, 100, 17)];
    _TimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(94, 71, 75, 17)];
    _Post_IMAGE = [[CHTVC_ImageView alloc]initWithFrame:CGRectMake(9, 7, 77, 81)];

    self.Header.lineBreakMode = NSLineBreakByWordWrapping;
    self.Header.numberOfLines = 0;
    //self.Header.translatesAutoresizingMaskIntoConstraints = NO;

    self.Subtitle.lineBreakMode = NSLineBreakByWordWrapping;
    self.Subtitle.numberOfLines = 0;
    //self.Subtitle.translatesAutoresizingMaskIntoConstraints = NO;


      //self.CommentLabel.translatesAutoresizingMaskIntoConstraints = NO;
      //self.TimeLabel.translatesAutoresizingMaskIntoConstraints = NO;
      //self.Post_IMAGE.translatesAutoresizingMaskIntoConstraints = NO;


            //adding the subviews
    [self.contentView addSubview:_Header];
    [self.contentView addSubview:_Subtitle];
    [self.contentView addSubview:_CommentLabel];
    [self.contentView addSubview:_TimeLabel];
    [self.contentView addSubview:_Post_IMAGE];
    }

但在编写以下代码行以添加约束之后:

    NSArray * CELLconstraints = @[[NSLayoutConstraint  constraintsWithVisualFormat:@"H:|-[Post_IMAGE]-8-[Header]-20-|" options:0 metrics:nil views:@{@"Post_IMAGE":self.Post_IMAGE,@"Header":self.Header}],
                              [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[Header]" options:0 metrics:nil views:@{@"Header": self.Header}],
                              [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[Post_IMAGE]-20-|" options:0 metrics:nil views:@{@"Post_IMAGE": self.Post_IMAGE}],
                              [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[Post_IMAGE]-8-[Subtitle]-20-|" options:0 metrics:nil views:@{@"Post_IMAGE": self.Post_IMAGE,@"Subtitle":self.Subtitle}],
                              [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[Subtitle]-20-|" options:0 metrics:nil views:@{@"Subtitle":self.Subtitle}],
                              [NSLayoutConstraint constraintsWithVisualFormat:@"H:[Post_IMAGE(100@20)]" options:0 metrics:nil views:@{@"Post_IMAGE": self.Post_IMAGE}],
                              [NSLayoutConstraint constraintsWithVisualFormat:@"V:[Post_IMAGE(100@20)]" options:0 metrics:nil views:@{@"Post_IMAGE": self.Post_IMAGE}],
                              [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-200-[CommentLabel]-20-|" options:0 metrics:nil views:@{@"CommentLabel": self.CommentLabel}],
                              [NSLayoutConstraint constraintsWithVisualFormat:@"V:[CommentLabel]-8-|" options:0 metrics:nil views:@{@"CommentLabel": self.CommentLabel}],
                              [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-94-[TimeLabel]-151-|" options:0 metrics:nil views:@{@"TimeLabel": self.TimeLabel}],
                              [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[TimeLabel]-8-|" options:0 metrics:nil views:@{@"TimeLabel": self.TimeLabel}]];
    for (NSLayoutConstraint * constraints in CELLconstraints) {
        [self.contentView addConstraints:constraints];
    };

我的自定义单元格子视图未正确定位......加上我遇到约束错误....正如我在控制台中提到的那样

无论如何我知道我不知道如何使用自动布局正确放置视图.........有人可以帮我吗

请注意,uilabels和细胞的大小是动态的

0 个答案:

没有答案