gdzie tworzyć ograniczenia dla autolayout dla subclassed uitableviewcell?

Próbuję użyć autolayout dla podklasy uitableviewcell, którą tworzę, i byłbym wdzięczny za jakąś radę na temat metody umieszczenia kodu tworzenia ograniczenia układu. Przeszukiwałem i wszystkie informacje, które znalazłem, mówią o dodawaniu ograniczeń po dodaniu widoków podrzędnych w metodzie viewDidLoad. Z tego, co wiem, viewDidLoad nie jest opcją dla podklasy przeglądarek.

Używam programu budującego interfejs do tworzenia niestandardowej komórki i dynamicznego przydzielania jej w moim kodzie. Nic specjalnego ... Podklasuję komendę, dzięki której mogę dodać niestandardowy widok do komórki. Ponownie, nic szczególnego nie niszczy Ziemi ... Moja trudność pojawia się, gdy próbuję umieścić mój niestandardowy widok w odniesieniu do etykiety dodanej do komórki w konstruktorze interfejsu.

Jest to kod do tworzenia niestandardowego widoku widoku i dodawania go do widoku treści komórki:

- (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];
}

Jeśli umieściłem kod ograniczenia na końcu initSelf nic się nie dzieje. Pozycja mojego _customBadge pozostaje domyślna. Kiedy umieściłem kod ograniczenia w layoutSubviews, stosowane jest pozycjonowanie; ale jestem pewien, że to niewłaściwe miejsce. Oto kod:

- (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];
}

Czy ktoś może mi powiedzieć, gdzie powinien iść ten kod? Z pewnością tworzę zduplikowane ograniczenia za każdym razem, gdy dzieje się układ.

Dzięki

questionAnswers(1)

yourAnswerToTheQuestion