【发布时间】:2021-11-04 11:01:23
【问题描述】:
我知道关于这个问题有很多类似的主题。虽然,我已经先尝试了它们,但没有解决我的问题。
如下图所示,我尝试使用 UICollectionView 应用水平滚动视图。每个单元格都包含一个具有动态文本长度的 UIButton。
问题是某些单元格的宽度错误/短。我尝试先获取每个 NSString 文本大小的解决方案,然后将其设置为 sizeForItemAt 委托方法。但是效果不好,即使我手动添加了填充值CGSize sizeWithPadding = CGSizeMake(size.width + 30, 40),文本仍在缩小。
经过几个小时的搜索和测试,仍然不知道如何解决这个问题。欢迎任何建议。
数据源 = NSString 数组
[NSMutableArray arrayWithObjects:@"All", @"Computer programming", @"Taylor Swift", @"Movies", @"Airplane", @"Basketball", @"Iron man", @"Football", @"ABC", @"Gong Fu", @"Dong Hua", nil]
一个 UIView 作为 collectionView 的容器。
- (void)setViews {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumLineSpacing = CGFLOAT_MAX;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
self.collectionView.translatesAutoresizingMaskIntoConstraints = false;
self.collectionView.backgroundColor = UIColor.systemBackgroundColor;
_collectionView.dataSource = self;
_collectionView.delegate = self;
[_collectionView registerClass:MenuBarCell.class forCellWithReuseIdentifier:menuBarCellID];
[self addSubview:self.collectionView];
[self.collectionView.topAnchor constraintEqualToAnchor:self.topAnchor].active = true;
[self.collectionView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = true;
[self.collectionView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = true;
[self.collectionView.heightAnchor constraintEqualToAnchor:self.heightAnchor].active = true;
}
# pragma mark - collectionView dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _menuBarArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MenuBarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:menuBarCellID forIndexPath:indexPath];
NSString *model = _menuBarArray[indexPath.item];
[cell configure:model];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = [self.menuBarArray[indexPath.item] sizeWithAttributes:NULL];
NSLog(@"size: width = %f", size.width);
CGSize sizeWithPadding = CGSizeMake(size.width + 30, 40);
return sizeWithPadding;
}
我的 UICollectionView 自定义单元格
- (void)setViews {
[super setViews];
self.contentView.backgroundColor = UIColor.systemYellowColor;
// create buttons
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.translatesAutoresizingMaskIntoConstraints = false;
_button.layer.cornerRadius = 15;
_button.layer.masksToBounds = true;
[_button setBackgroundColor:[UIColor colorWithWhite:0.92 alpha:1]];
[_button setTitleColor:UIColor.labelColor forState:UIControlStateNormal];
[_button setTitleColor:UIColor.buttonTitleHighlighted forState:UIControlStateHighlighted];
[self.contentView addSubview:_button];
// set constraints
[_button.centerXAnchor constraintEqualToAnchor:self.contentView.centerXAnchor].active = true;
[_button.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor].active = true;
[_button.widthAnchor constraintEqualToAnchor:self.contentView.widthAnchor].active = true;
[_button.heightAnchor constraintLessThanOrEqualToAnchor:self.contentView.heightAnchor].active = true;
}
- (void)configure: (NSString *)model {
[_button setTitle:model forState:UIControlStateNormal];
}
【问题讨论】:
-
您的代码是您计算大小并通过委托方法设置它。那么 'sizeWithAttributes:NULL' 为什么要使用 NULL,我们应该使用正确的字体属性吗?
-
实际上我尝试给它一个字体大小或
NULL,虽然它没有包含在上面的代码中,但也不能给我一个正确的标题文本的 UIButton 宽度。请参阅 Don Mag 的回答,它非常有效。
标签: ios objective-c uicollectionview