【问题标题】:UICollectionViewCell contentView paddingUICollectionViewCell contentView 填充
【发布时间】:2018-09-02 00:15:31
【问题描述】:

我想从自定义UICollectionViewCell 内部为contentView 设置填充,使其小于单元格本身。 我尝试通过为contentView 设置锚点来做到这一点,但这个视图似乎总是与单元格大小相同。

这是我尝试过的。

 self.contentView.translatesAutoresizingMaskIntoConstraints = false;
 [self.contentView.topAnchor constraintEqualToAnchor:self.topAnchor constant:5].active = true;
 [self.contentView.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:5].active = true;
 [self.contentView.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:-5].active = true;
 [self.contentView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-5].active = true;

为了快速解决问题,我在contentView 中创建了另一个视图,我可以按照上面显示的方式锚定,但我希望有一个更清洁的解决方案。

【问题讨论】:

  • contentView 中添加一个视图并为该视图添加约束而不是contentView

标签: ios objective-c uicollectionview uicollectionviewcell padding


【解决方案1】:

您可能想尝试为contentView 设置edge 插入,但如果您采用这种方法,您应该将subviews 固定到布局边距指南而不是直接锚点。

我来自 Swift,但我想你会理解我的:

contentView.layoutMargins = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
someSubview.topAnchor.constraintEqualToAnchor(contentView.layoutMarginsGuide.topAnchor).isActive = true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2015-11-07
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多