2013-04-09 96 views
15

我想为我创建的uitableviewcell子类使用自动布局,我将不胜感激关于什么方法来放置布局约束创建代码的一些建议。我一直在四处搜寻,并且在viewDidLoad方法中添加子视图后,我找到了所有关于添加约束的信息。据我所知,viewDidLoad不适用于uitableviewcell子类。在哪里创建subclassed 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]; 
} 

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

感谢

回答

31

您将需要更新喜欢的约束:

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

加入您的意见,上海华盈后,你需要调用[self setNeedsUpdateConstraints]开始使用它们。通过这样做,渲染运行时将在正确的时间调用updateConstraints

+1

该文档(https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instm/UIView/updateConstraints)说,“调用[super updateConstraints]作为实现的最后一步。“ – cnotethegr8 2013-10-18 07:09:53

+26

但是,每次调用updateConstraints时,这是否会重新添加相同的约束?在再次添加之前先删除先前添加的任何约束是否是最佳做法?或者,iOS是否足够明智地认识到您添加的约束已经存在,因此不会再添加它? – PapillonUK 2013-10-21 22:42:45

+0

我想,不应该只在需要时调用setNeedsUpdateConstraints ... – Adrian 2013-10-22 09:10:26