【问题标题】:where to create autolayout constraints for subclassed uitableviewcell?在哪里为子类 uitableviewcell 创建自动布局约束?
【发布时间】:2013-03-31 10:32:04
【问题描述】:

我正在尝试将自动布局用于我正在创建的 uitableviewcell 子类,我希望能提供一些关于放置布局约束创建代码的方法的建议。我一直在四处搜索,我找到的所有信息都谈到了在 viewDidLoad 方法中添加子视图后添加约束。据我所知 viewDidLoad 不是 uitableviewcell 子类的选项。

我正在使用界面生成器来创建自定义单元格并在我的代码中动态分配它。没什么特别的...我将 uitableviewcell 子类化,以便可以将自定义 uiview 添加到单元格。同样,没有什么特别惊天动地的......当我尝试根据我添加到界面构建器中的单元格的标签来定位我的自定义 uiview 时,我遇到了困难。

这是创建自定义 uiview 并将其添加到单元格内容视图的代码:

- (id)initWithCoder:(NSCoder *)decoder
{
    if ((self = [super initWithCoder:decoder]))
    {
        [self initSelf];
    }
    return self;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
    {
        [self initSelf];
    }
    return self;
}

- (void) initSelf
{
    // Initialization code
    _badgeLabel = @"";

    if (!_customBadge)
    {
        _customBadge = [CustomBadge customBadgeWithString:self.badgeLabel];
    }

    // hide the badge until needed
    self.customBadge.hidden = YES;

    // add badge to the cell view hierarchy
    [self.customBadge setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.customBadge setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.customBadge setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];

    [self.contentView addSubview:self.customBadge];
}

如果我将约束代码放在 initSelf 的末尾,则不会发生任何事情。我的 _customBadge 的位置保持默认。当我将约束代码放在 layoutSubviews 中时,应用了定位;但我很确定这是错误的地方。代码如下:

- (void) layoutSubviews
{
    [self.contentView addConstraint:[NSLayoutConstraint
                                     constraintWithItem:self.customBadge
                                     attribute:NSLayoutAttributeLeft
                                     relatedBy:NSLayoutRelationEqual
                                     toItem:self.competence
                                     attribute:NSLayoutAttributeRight
                                     multiplier:1.0
                                     constant:-14.0]];

    [self.contentView addConstraint:[NSLayoutConstraint
                                     constraintWithItem:self.customBadge
                                     attribute:NSLayoutAttributeTop
                                     relatedBy:NSLayoutRelationEqual
                                     toItem:self.competence
                                     attribute:NSLayoutAttributeTop
                                     multiplier:1.0
                                     constant:0.0]];

    [super layoutSubviews];
}

谁能告诉我这段代码应该去哪里?当然,每次布局发生时我都会创建重复的约束。

谢谢

【问题讨论】:

    标签: ios uitableview constraints autolayout layoutsubviews


    【解决方案1】:

    您需要更新约束,例如:

    -(void)updateConstraints{
     // add your constraints if not already there
     [super updateConstraints];
    }
    

    将视图添加到 superview 后,您需要致电 [self setNeedsUpdateConstraints] 开始使用它们。通过这样做,渲染运行时将在正确的时间调用updateConstraints

    【讨论】:

    • 文档 (developer.apple.com/library/ios/documentation/uikit/reference/…) 说,“调用 [super updateConstraints] 作为实现的最后一步。”。
    • 但这是否会在每次调用 updateConstraints 时重新添加相同的约束?在再次添加之前先删除之前添加的任何约束是最佳做法吗?或者 iOS 是否足够明智地认识到您正在添加的约束已经存在,因此不会再次添加它?
    • @PapillonUK:我看到其他实现使用bool 来检查是否已经添加了约束。所以下次不会再添加了。当然,如果发生变化(例如方向变化),您必须删除/添加/更改约束。
    • @PapillonUK 的问题让我感到好奇,@testing 谈到了一个布尔标志。更具体地说,@smileyborg 在另一个 SO 答案中解决了这个问题。作为参考,我在这里链接(他对updateConstraints 的回复包含在他接受的答案的第 1 点和第 2 点中)链接:stackoverflow.com/questions/19015215/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多