2013-04-09 10 views
15

私は作成しているuitableviewcellサブクラスにautolayoutを使用しようとしていますが、レイアウト制約の作成コードをどのように置くべきかについてのアドバイスをいただければ幸いです。私は、検索してきたすべての情報は、viewDidLoadメソッドでサブビューを追加した後に制約を追加することについて話しています。私の知る限り、viewDidLoadはuitableviewcellサブクラスのオプションではありません。サブクラス化されたuitableviewcellの自動レイアウト制約を作成する場所

私はインターフェイスビルダを使用してカスタムセルを作成し、それを自分のコードに動的に割り当てます。何も特別なことはありません...セルにカスタムuiviewを追加できるように、私はuitableviewcellをサブクラス化します。繰り返しますが、特に地球が壊れているものは何もありません...私はインターフェイスビルダーのセルに追加したラベルに関してカスタムUIを配置しようとします。

- (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の終わりに制約コードを置く場合:

この

は、カスタムUIViewのを作成して、セルのコンテンツビューに追加するためのコードです。私の_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]; 
} 

誰でもこのコードのどこに行かなければならないのですか?確かに、レイアウトが発生するたびに重複制約を作成しています。あなたがそれらを使用して起動する[self setNeedsUpdateConstraints]を呼び出す必要がスーパーためにあなたの意見を追加した後

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

:あなたが好きな制約を更新する必要があります

おかげ

答えて

31

。これにより、レンダリングランタイムは、適切なタイミングでupdateConstraintsを呼び出します。

+1

ドキュメント(https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instm/UIView/updateConstraints)では、「Call [super updateConstraints]を実装の最終ステップとして使用してください。 " – cnotethegr8

+26

しかし、これはupdateConstraintsが呼び出されるたびに同じ制約を再追加しますか?以前に追加した制約を最初に削除してから再度追加することをお勧めしますか?または、あなたが追加している制約が既に存在しているので、それをもう一度追加しないことを認識するのに十分なのですか? – PapillonUK

+0

必要がある場合にのみsetNeedsUpdateConstraintsを呼び出すべきではないと思います... – Adrian

関連する問題