Я работаю с AFCollectionView в таблице, и это не рендеринг, как я ожидал бы. Чтобы отладить проблему, я создал другую пользовательскую ячейку таблицы с желаемым расположением и поместил ее в ячейки AFCollectionView. Ячейки просмотра коллекции все еще не отображаются правильно.Что приводит к тому, что два UITableViewCells с одинаковыми ограничениями автоопределения ведут себя по-другому?
Обе эти ячейки были построены без xib или раскадровки, поэтому я пытаюсь отлаживать проблемы с автозагрузкой. Я понимаю, что UICollectionView более сложный, чем UIView, но я не уверен, где даже начать пытаться внести изменения, учитывая отображаемые проблемы.
Обе клетки имеют следующий метод, называемый updateConstraints изнутри - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
метода.
[self updateConstraints];
return self;
}
- (void)updateConstraints{
[super updateConstraints];
//Create local versions of the ivars
UILabel *titleLabelP = self.titleLabel;
UIView *bottomViewP = self.collectionView;
NSNumber *bottomViewHeightP = [NSNumber numberWithInteger:self.bottomViewHeight];
//Build the visual constraints
NSDictionary *views = NSDictionaryOfVariableBindings(titleLabelP, bottomViewP);
NSDictionary *metrics = @{ @"padding" : @8.0, @"viewHeight": bottomViewHeightP };
// title and bottom view fill the width of the superview (cell content view)
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-padding-[titleLabelP]-padding-|" options:0 metrics:metrics views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-padding-[bottomViewP]-padding-|" options:0 metrics:metrics views:views]];
// title and bottom view are setup vertically with 8px of padding between. The cell should expand to fit the full size of the bottom view.
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-padding-[titleLabelP]-padding-[bottomViewP(viewHeight)]-padding-|" options:0 metrics:metrics views:views]];
}
CONSOLE Логарифм AUTOLAYOUT ВОПРОСЫ:
2016-11-21 14:19:06.423003 AutoLayoutTableCellTest[71645:4731377] [Warning] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
2016-11-21 14:19:06.425136 AutoLayoutTableCellTest[71645:4731377] [LayoutConstraints] 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:0x608000096710 h=--& v=--& UITableViewCellContentView:0x7fb87ac1a770.midY == 22.25 (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x6080000966c0 h=--& v=--& UITableViewCellContentView:0x7fb87ac1a770.height == 44.5 (active)>",
"<NSLayoutConstraint:0x608000095860 V:|-(8)-[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1'] (active, names: '|':AFTableViewCell:0x7fb87b050400'CellIdentifier')>",
"<NSLayoutConstraint:0x608000095900 AFIndexedCollectionView:0x7fb87b051000.height == 150 (active)>",
"<NSLayoutConstraint:0x608000095950 V:[AFIndexedCollectionView:0x7fb87b051000]-(8)-| (active, names: '|':UITableViewCellContentView:0x7fb87ac1a770)>",
"<NSLayoutConstraint:0x608000095fe0 V:[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1']-(8)-[AFIndexedCollectionView:0x7fb87b051000] (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x608000095180 h=-&- v=--& 'UIView-Encapsulated-Layout-Top' AFTableViewCell:0x7fb87b050400'CellIdentifier'.minY == 0 (active, names: '|':UITableViewWrapperView:0x7fb87b03b800)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x608000095fe0 V:[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1']-(8)-[AFIndexedCollectionView:0x7fb87b051000] (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-11-21 14:19:06.425934 AutoLayoutTableCellTest[71645:4731377] [LayoutConstraints] 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:0x608000096710 h=--& v=--& UITableViewCellContentView:0x7fb87ac1a770.midY == 22.25 (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x6080000966c0 h=--& v=--& UITableViewCellContentView:0x7fb87ac1a770.height == 44.5 (active)>",
"<NSLayoutConstraint:0x608000095860 V:|-(8)-[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1'] (active, names: '|':AFTableViewCell:0x7fb87b050400'CellIdentifier')>",
"<NSLayoutConstraint:0x6080000958b0 V:[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1']-(8)-[AFIndexedCollectionView:0x7fb87b051000] (active)>",
"<NSLayoutConstraint:0x608000095900 AFIndexedCollectionView:0x7fb87b051000.height == 150 (active)>",
"<NSLayoutConstraint:0x608000095950 V:[AFIndexedCollectionView:0x7fb87b051000]-(8)-| (active, names: '|':UITableViewCellContentView:0x7fb87ac1a770)>",
"<NSAutoresizingMaskLayoutConstraint:0x608000095180 h=-&- v=--& 'UIView-Encapsulated-Layout-Top' AFTableViewCell:0x7fb87b050400'CellIdentifier'.minY == 0 (active, names: '|':UITableViewWrapperView:0x7fb87b03b800)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6080000958b0 V:[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1']-(8)-[AFIndexedCollectionView:0x7fb87b051000] (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
Я поставил образец проекта на GitHub @https://github.com/propstm/AutoLayoutTableCellTest
EDIT ПОКАЗАТЬ INIT ДЛЯ КЛЕТОК С CollectionView:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(10, 10, 9, 10);
layout.itemSize = CGSizeMake(100, 100);
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView = [[AFIndexedCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
[self.collectionView setTranslatesAutoresizingMaskIntoConstraints:FALSE];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.showsHorizontalScrollIndicator = NO;
[self.contentView addSubview:self.collectionView];
if(!self.titleLabel){
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 8, self.frame.size.width-8, 20)];
[self.titleLabel setTranslatesAutoresizingMaskIntoConstraints:FALSE];
UIFont *boldFont = [UIFont fontWithName:@".SFUIText-Bold" size:16.0f];
[self.titleLabel setFont:boldFont];
[self addSubview:self.titleLabel];
}
self.bottomViewHeight = 150;
[self updateConstraints];
return self;
}
У меня была эта часть выше в методе init ячейки. Я добавил этот код и в сообщение. Я попробовал заменить NO с FALSE, но без разрешения. – propstm
Я использую оценочные высоты строк. TitleLabel имеет высоту 20, высота коллекцииView равна 150, поэтому с моим вертикальным ограничением я ожидаю, что высота авто размера составит около 200. – propstm
У вас также есть paddings. Верните точную высоту суммы всех высот и прокладок – Ryan