我正在尝试使用UICollectionView制作自定义工具栏。
我想子类化UIView并将集合视图作为子子视图。
我使用的是标准流程布局,如果将集合视图添加到视图控制器中,则一切工作正常。
当我将UICollectionview添加到MY子类视图时,所有这些都将停止正常工作。这些项目首先出现在视图外部,我必须向下滚动才能看到它们。项目开始之前似乎要添加一行空白。
当我在UIView子类内的collectionview上调用contentSize时,由于某种原因它会返回-20的高度。
我将工具集实例指向直接在视图控制器视图中托管的CollectionView的数据源,这样就可以排除代码差异。我在想,我可能会错过一些与UIView生命周期事件有关的细微问题?
这是代码:
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } -(void)updateConstraints { [super updateConstraints]; UIView* view = _collectionView; [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeLeading multiplier:1 constant:0]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]]; } -(void)layoutSubviews { [super layoutSubviews]; [_collectionView reloadData]; } +(BOOL)requiresConstraintBasedLayout { return YES; } -(id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { _itemsVisibleAtOnce = 5; // dispatch_async(dispatch_get_main_queue(), ^{ UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init]; // layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; UICollectionView* collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout]; _collectionView = collectionView; [self.collectionView registerNib:[UINib nibWithNibName:@"OSButtonBarCell" bundle:nil] forCellWithReuseIdentifier:REUSUE_OS_TAB_CELL]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.translatesAutoresizingMaskIntoConstraints = NO; [self addSubview:_collectionView]; // [self invalidateIntrinsicContentSize]; [self setNeedsUpdateConstraints]; // }); } return self; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { NSUInteger numItemsHorizontal = [self collectionView:collectionView numberOfItemsInSection:indexPath.section]; if (_itemsVisibleAtOnce < numItemsHorizontal) { numItemsHorizontal = _itemsVisibleAtOnce; } CGFloat itemWidth = collectionView.bounds.size.width / numItemsHorizontal; return CGSizeMake(itemWidth, 30); } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 0; } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 0; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { OSButtonBarItem* button = _buttons[indexPath.row]; OSButtonBarCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:REUSUE_OS_TAB_CELL forIndexPath:indexPath]; // cell.translatesAutoresizingMaskIntoConstraints = NO; cell.contentView.translatesAutoresizingMaskIntoConstraints = NO; cell.indexPath = indexPath; cell.titleLabel.text = button.text; cell.backgroundColor = button.backgroundColour; return cell; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _buttons.count; }
如果有人对可能发生的事情有任何建议,我将非常感激!
非常感谢克里斯