j'ai commencé à utiliser autolayout et les contraintes programatically, mais je ne comprends pas pourquoi je reçois l'avertissement suivantne peut satisfaire les contraintes - Format du langage Visual
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:0x7fbbd142ead0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fbbd162ec70(44)]>",
"<NSLayoutConstraint:0x7fbbd1431740 V:|-(20)-[UILabel:0x7fbbd174e520'Liked'] (Names: '|':UITableViewCellContentView:0x7fbbd162ec70)>",
"<NSLayoutConstraint:0x7fbbd1431790 V:[UILabel:0x7fbbd174e520'Liked']-(NSSpace(8))-[UILabel:0x7fbbd174e7e0's']>",
"<NSLayoutConstraint:0x7fbbd14317e0 V:[UILabel:0x7fbbd174e7e0's']-(20)-| (Names: '|':UITableViewCellContentView:0x7fbbd162ec70)>"
)
Je sais que la ligne de code qui est à l'origine de l'erreur est:
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[nameLbl]-[notificationLbl]-[textLabel]-20-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:views];
mais je ne comprends pas pourquoi la contrainte ne peut pas être satisfaite. Cela peut être une question stupide, mais je suis nouveau à autolayout et le sujet est confus, à cause de la documentation incomplète. J'ai essayé différentes variantes et solutions mais je n'ai pas trouvé la bonne. Qu'est-ce que je rate? Je colle également le code de ma cellule de vue de table personnalisée pour une inspection plus détaillée.
@implementation NotificationCell
@synthesize nameLbl,notificationLbl,textLabel,timeLbl,profileImgView,notificationImgView,clockImgView,didSetupConstraints;
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
nameLbl = [[UILabel alloc]init];
[nameLbl setTextColor:[UIColor blueColor]];
nameLbl.translatesAutoresizingMaskIntoConstraints = NO;
[nameLbl setTextAlignment:NSTextAlignmentLeft];
[nameLbl setNumberOfLines:1];
[nameLbl setBackgroundColor:[UIColor redColor]];
[self.contentView addSubview:nameLbl];
notificationLbl = [[UILabel alloc]init];
[notificationLbl setTextColor:[UIColor lightGrayColor]];
notificationLbl.translatesAutoresizingMaskIntoConstraints = NO;
[notificationLbl setTextAlignment:NSTextAlignmentLeft];
[notificationLbl setNumberOfLines:1];
[self.contentView addSubview:notificationLbl];
textLabel = [[UILabel alloc]init];
[textLabel setTextColor:[UIColor blueColor]];
textLabel.translatesAutoresizingMaskIntoConstraints = NO;
[textLabel setTextAlignment:NSTextAlignmentLeft];
[textLabel setNumberOfLines:0];
[textLabel setBackgroundColor:[UIColor grayColor]];
[self.contentView addSubview:textLabel];
timeLbl = [[UILabel alloc]init];
[timeLbl setTextColor:[UIColor grayColor]];
timeLbl.translatesAutoresizingMaskIntoConstraints = NO;
[timeLbl setTextAlignment:NSTextAlignmentLeft];
[timeLbl setNumberOfLines:1];
[self.contentView addSubview:timeLbl];
profileImgView = [[UIImageView alloc]init];
[profileImgView setTranslatesAutoresizingMaskIntoConstraints:NO];
[profileImgView setBackgroundColor:[UIColor redColor]];
[profileImgView.layer setCornerRadius:20];
[profileImgView.layer setMasksToBounds:YES];
[self.contentView addSubview:profileImgView];
clockImgView = [[UIImageView alloc]init];
[clockImgView setTranslatesAutoresizingMaskIntoConstraints:NO];
[clockImgView setBackgroundColor:[UIColor blueColor]];
[clockImgView.layer setCornerRadius:10];
[clockImgView.layer setMasksToBounds:YES];
[self.contentView addSubview:clockImgView];
notificationImgView = [[UIImageView alloc]init];
[notificationImgView setTranslatesAutoresizingMaskIntoConstraints:NO];
[notificationImgView setBackgroundColor:[UIColor purpleColor]];
[notificationImgView.layer setCornerRadius:10];
[notificationImgView.layer setMasksToBounds:YES];
[self.contentView addSubview:notificationImgView];
/*[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_headerView(==80)]-0-[_tableView]-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:elementsDict]];*/
}
return self;
}
-(void)updateConstraints{
if (self.didSetupConstraints == NO) {
NSDictionary *views = NSDictionaryOfVariableBindings(nameLbl,notificationLbl,notificationImgView,textLabel,timeLbl,profileImgView,clockImgView);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[profileImgView(40)]-10-[textLabel]-[clockImgView(20)]-[timeLbl(20)]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[profileImgView]-10-[nameLbl]-[clockImgView]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[profileImgView]-10-[notificationImgView(20)]-[notificationLbl]-[clockImgView]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[nameLbl]-[notificationLbl]-[textLabel]-20-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[notificationImgView(20)]-[textLabel]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[profileImgView(40)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[clockImgView(20)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[timeLbl]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:views];
[self.contentView addConstraints:constraints];
self.didSetupConstraints = YES;
}
[super updateConstraints];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self.contentView setNeedsLayout];
[self.contentView layoutIfNeeded];
self.textLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.textLabel.frame);
}
Quelles versions iOS devez-vous prendre en charge? (Ce comportement a changé un peu dans iOS 8.) Quelle version iOS était en cours d'exécution lorsque vous avez reçu les avertissements ci-dessus? En outre, je suppose que vous n'utilisez pas de prototypes de cellules, mais que vous avez plutôt un 'cellForRowAtIndexPath' qui l'instancie manuellement? Peut-être partager comment la cellule est créée, car le comportement diffère en fonction de la façon dont la cellule a été créée. – Rob
J'ai une cellule prototype mais je la crée par programmation comme vous pouvez le voir dans le code ci-dessus, alors quel est votre point de vue? Je suis en train de tester sur iOS 8. – axel
Si vous utilisez un prototype de cellule, il instancie la cellule avec 'initWithCoder', pas' initWithStyle', ce qui explique pourquoi j'ai posé cette question. J'ai seulement posé des questions sur le prototype parce que (en particulier iOS 7), le comportement dépend fortement de la façon dont vous avez créé la cellule, et vous ne l'avez pas partagé avec nous. Sur un sujet distinct, je suis surpris que vous n'ayez pas appelé 'setNeedsUpdateConstraints' après avoir ajouté les sous-vues (c'est ce que l'on appelle généralement pour que l'OS appelle' updateConstraints' pour vous, mais comment cette méthode est appelée). – Rob