Próbuję zbudować titleView z ograniczeniami, który wygląda tak:Budowanie titleView programowo z ograniczeniami (lub ogólnie konstruowania widok z ograniczeniami)
wiem, jak bym to zrobić ramki. Chciałbym obliczyć szerokość tekstu, szerokość obrazu, utworzyć widok o tej szerokości/wysokości, aby zawierał oba, a następnie dodać oba jako subviews w odpowiednich miejscach z ramkami.
Próbuję zrozumieć, jak można to zrobić z ograniczeniami. Pomyślałem, że wewnętrzny rozmiar treści pomógłby mi tutaj, ale ja wymiotuję dookoła, próbując uruchomić to.
UILabel *categoryNameLabel = [[UILabel alloc] init];
categoryNameLabel.text = categoryName; // a variable from elsewhere that has a category like "Popular"
categoryNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
[categoryNameLabel sizeToFit]; // hoping to set it to the instrinsic size of the text?
UIView *titleView = [[UIView alloc] init]; // no frame here right?
[titleView addSubview:categoryNameLabel];
NSArray *constraints;
if (categoryImage) {
UIImageView *categoryImageView = [[UIImageView alloc] initWithImage:categoryImage];
[titleView addSubview:categoryImageView];
categoryImageView.translatesAutoresizingMaskIntoConstraints = NO;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[categoryImageView]-[categoryNameLabel]|" options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(categoryImageView, categoryNameLabel)];
} else {
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[categoryNameLabel]|" options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(categoryNameLabel)];
}
[titleView addConstraints:constraints];
// here I set the titleView to the navigationItem.titleView
Nie powinieneś wymagać twardego kodu rozmiaru titleView. Powinno być możliwe do określenia poprzez rozmiar jego zawartości, ale ...
- TitleView określa, że jego rozmiar to 0, chyba że utknę w ramkę.
- Jeżeli ustawić
translatesAutoresizingMaskIntoConstraints = NO
Aplikacja ulega awarii z tego błędu:'Auto Layout still required after executing -layoutSubviews. UINavigationBar's implementation of -layoutSubviews needs to call super.'
aktualizacji
Dostałem go do pracy z tym kodem, ale ciągle mam ustawić ramkę na titleView:
UILabel *categoryNameLabel = [[UILabel alloc] init];
categoryNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
categoryNameLabel.text = categoryName;
categoryNameLabel.opaque = NO;
categoryNameLabel.backgroundColor = [UIColor clearColor];
UIView *titleView = [[UIView alloc] init];
[titleView addSubview:categoryNameLabel];
NSArray *constraints;
if (categoryImage) {
UIImageView *categoryImageView = [[UIImageView alloc] initWithImage:categoryImage];
[titleView addSubview:categoryImageView];
categoryImageView.translatesAutoresizingMaskIntoConstraints = NO;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[categoryImageView]-7-[categoryNameLabel]|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(categoryImageView, categoryNameLabel)];
[titleView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[categoryImageView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(categoryImageView)];
[titleView addConstraints:constraints];
titleView.frame = CGRectMake(0, 0, categoryImageView.frame.size.width + 7 + categoryNameLabel.intrinsicContentSize.width, categoryImageView.frame.size.height);
} else {
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[categoryNameLabel]|" options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(categoryNameLabel)];
[titleView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[categoryNameLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(categoryNameLabel)];
[titleView addConstraints:constraints];
titleView.frame = CGRectMake(0, 0, categoryNameLabel.intrinsicContentSize.width, categoryNameLabel.intrinsicContentSize.height);
}
return titleView;
Twój oryginalny kod prawdopodobnie nie działał, ponieważ nie masz żadnych ograniczeń w osi pionowej; twoja druga powinna wystarczyć bez ustawiania żadnych ramek. Co się stanie, jeśli utworzysz ten widok i dodasz go gdzie indziej (zamiast do paska nawigacji, który może wprowadzać dodatkową złożoność). – jrturton
Próbuję osiągnąć to bez ustawiania ramki, ale nie mogłem znaleźć, kto jest rodzicem widoku tytułu, w którym powinniśmy dodać ograniczenia ... –
Rodzicem widoku jest sam 'UINavigationBar' - ale jesteś niedozwolone dodawanie do niego ograniczeń z jakiegoś powodu - niepowodzenie z 'Nie można zmodyfikować ograniczeń dla UINavigationBar zarządzanego przez kontroler'. – Petar