因此,我正在使用我在Github上找到的項目 - https://github.com/kmonaghan/CBPWordPress - 並試圖獲得它的一個句柄。NSLayoutConstraint,在表格視圖單元格中不工作的子查看UIView
本質上,我想要做的是創建像Facebook這樣的應用程序的浮動「卡」錯覺。我在過去完成這項工作的方式是在將標題標籤,作者名稱放入該視圖之前,創建子視圖UIView,四捨五入等。問題在於,我通常是用故事板完成的,而這個例子通過編程來解決問題。
在玩弄它之後,我想我已經弄明白了。這是我如何設置 - (空)updateConstraints方法:
(無效)updateConstraints { 如果(self.constraintsUpdated!){
self.contentView.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.frame), CBPLargePostPreviewTableViewCellHeight); NSDictionary *views = @{@"postCommentLabel": self.postCommentLabel, @"postDateLabel": self.postDateLabel, @"postImageView": self.postImageView, @"postTitleLabel": self.postTitleLabel, @"roundedBackground": self.roundedBackground}; NSDictionary *metrics = @{@"padding": @(CBPPadding)}; [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(4)-[roundedBackground]-(4)-|" options:0 metrics:metrics views:views]]; [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(4)-[roundedBackground]-(4)-|" options:0 metrics:metrics views:views]]; [self.roundedBackground addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(5)-[postTitleLabel]-(5)-[postImageView(150)]-(5)-[postDateLabel]-(5)-|" options:0 metrics:nil views:views]]; [self.roundedBackground addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding)-[postTitleLabel]-(padding)-|" options:0 metrics:metrics views:views]]; [self.roundedBackground addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding)-[postImageView]-(padding)-|" options:0 metrics:metrics views:views]]; [self.roundedBackground addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding)-[postDateLabel]-(>=0)-[postCommentLabel]-(padding)-|" options:0 metrics:metrics views:views]]; [self.roundedBackground addConstraint:[NSLayoutConstraint constraintWithItem:self.postCommentLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.postDateLabel attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0]]; self.constraintsUpdated = YES; } [super updateConstraints]; }
但在我運行得到這個錯誤:[__NSPlaceholderDictionary initWithObjects:forKeys:count:]:嘗試從對象插入零對象[4]'
我已經嘗試創建視圖的邊界,但無濟於事。有人有任何建議嗎?如果有更好的方法來創造我期待的效果,我也對此開放。
對不起(我改變了一些代碼,當我複製/粘貼它,不是一個好主意)。儘管如此,即使是正確的,我仍然得到我描述的錯誤。 – schmorgledorf 2014-12-07 21:01:24