我正在使用UIScrollView
我希望显示在我们的应用中输入的用户卡。我使用名为CustomView
的{{1}}来显示卡片信息。问题我' m具有的是当我设置宽度SavedCardView
时,滚动视图停止滚动,否则它可以根据子视图自动调整SavedCardView
宽度。我使用以下代码将SaveCardView
添加到SavedCardView
,这是SavedCardsContentView
的子视图,其顶部,底部,前导和&尾随约束与scrollview对齐。我已将“SavedCardsContentView”的高度设置为180分。
UIScrollView
我不知道为什么在设置//Add the card views
SavedCardView *previousView = nil;
for ( Card *card in _savedCards ) {
//Load the card view from the xib file
// Instantiate the nib content without any reference to it.
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"SavedCardView" owner:nil options:nil];
// Find the view among nib contents (not too hard assuming there is only one view in it).
SavedCardView *cardView = [nibContents lastObject];
// Add the user card to saved cardView
[cardView updateViews:card];
//Set the translate auto resizing mask to false
[cardView setTranslatesAutoresizingMaskIntoConstraints:NO];
//Add the view to the events container
[_savedCardsContentView addSubview:cardView];
//[cardView setSaveCardViewDelegate:self];
// Set width constraint
[cardView addConstraint:[NSLayoutConstraint constraintWithItem:cardView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:260]];
//Start adding the remaining constraints
NSLayoutConstraint *leftConstraint;
if ( !previousView ) {
//Add the left constraint to the parent view
leftConstraint = [NSLayoutConstraint constraintWithItem:cardView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_savedCardsContentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:20];
} else {
//Add the left constraint to the previous view
leftConstraint = [NSLayoutConstraint constraintWithItem:cardView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:previousView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:20];
}
[_savedCardsContentView addConstraint:leftConstraint];
//Add the top and bottom constraint
[_savedCardsContentView addConstraint:[NSLayoutConstraint constraintWithItem:_savedCardsContentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cardView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
[_savedCardsContentView addConstraint:[NSLayoutConstraint constraintWithItem:_savedCardsContentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:cardView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
if ( [card isEqual:[_savedCards lastObject]] ) {
//Add the trailing constraint to the cards content view
[_savedCardsContentView addConstraint:[NSLayoutConstraint constraintWithItem:_savedCardsContentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:cardView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
}
previousView = cardView;
}
宽度后它没有工作,任何帮助都表示赞赏。