2013-03-07 13 views
5

próbowałem za pomocą wiązań autoLayout w IB przed i rozumiem, jak one działają i które muszą być jednoznaczne etc ...Stosując NSLayoutConstraints do podklasy UIView

Ale gdy masz więcej niż kilka widoków robi się nieco skomplikowana i ma tendencję do pękania.

Tak, właśnie czytałem bloga o facecie, który ma ten sam problem i używa kodu ASCII, aby utworzyć ograniczenia.

Mam UITableView z niestandardowym UITableViewCell, który ma dynamiczny rozmiar w oparciu o ilość treści. Idealny kandydat do automatycznego układu.

Tak, próbowałem na kilka sposobów, a teraz ograniczyłem zawartość, aby w komórce była tylko jedna etykieta.

Co chciałbym, aby etykieta wypełniła komórkę obramowaniem.

tj. Standardowy rozmiar wskazuje z każdej krawędzi komórki.

Zrobiłem to tak ...

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 
     _label.textColor = [UIColor colorWithWhite:0.53 alpha:1.0]; 
     _label.backgroundColor = [UIColor yellowColor]; 
     [self addSubview:_label]; 

     NSDictionary *views = NSDictionaryOfVariableBindings(_label); 

     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_label]-|" 
                    options:0 
                    metrics:nil 
                     views:views]]; 

     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[_label]-|" 
                    options:0 
                    metrics:nil 
                     views:views]]; 

     self.backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; 
     self.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 

Ale gdy wyświetlany jest komórka etykieta znajduje się w lewym górnym rogu dokładnie jak CGRect Kiedyś to init, a ja się tego obciążenia błędów dotyczących ograniczeń ...

Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>", 
    "<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2013-03-07 11:56:14.841 unasys[13082:907] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2013-03-07 11:56:14.854 unasys[13082:907] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e80 h=--& v=--& H:[UILabel:0x1f864a00(10)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1ed8e150 h=--& v=--& H:[MyCell:0x1f857740(320)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2013-03-07 11:56:14.858 unasys[13082:907] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1eda7260 h=--& v=--& V:[MyCell:0x1f857740(143)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859f00 h=--& v=--& V:[UILabel:0x1f864a00(10)]>", 
    "<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)> 

i nie mam pojęcia od czego zacząć debugowania to. Ograniczenia są absolutnie minimalne, ale wciąż całkowicie zawodzą.

Czy ktoś może wskazać mi właściwy kierunek działania tego ograniczenia?

Odpowiedz

13

Po linii

_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 

dodać

[_label setTranslatesAutoresizingMaskIntoConstraints:NO]; 

Domyślnie autoresizingmask również przekształcony ograniczeń, w sposób sprzeczny z ograniczeniami określonymi pan, co niejednoznaczną

Dokumenty są tutaj: setTranslatesAutoresizingMaskIntoConstraints

+0

Awesome! Dziękuję bardzo! – Fogmeister

+0

Działa! Musiałem również zmienić, aby dodać rzeczy do self.contentView zamiast siebie, ponieważ jest to UITableViewCell, a następnie wszystko działało: D Dzięki! – Fogmeister

+1

Zasadniczo, jeśli zobaczysz "NSAutoresizingMaskLayoutConstraint" w błędzie autolayout i nie wiesz, dlaczego tam jest, zapomniałeś wyłączyć 'translatesAutoresizingMaskIntoConstraints' gdzieś – iain

Powiązane problemy