菜鸟教程小白 发表于 2022-12-12 13:13:45

ios - UICollectionView 单元格框架具有十进制原值


                                            <p><p>我有一个以编程方式创建的 UICollectionView。 Collection View 中的每个单元格都显示一个标签,并且所有标签都具有相同的属性。尽管如此,我注意到 Collection View 中心列中每个标签中包含的文本看起来很模糊。 </p>

<p> Collection View 的递归描述显示表格中的中心单元格始终有一个 x 原点,它是一个十进制值:</p>

<pre><code> &lt;UICollectionViewCell: 0xd98c1e0; frame = (242.5 0; 220 45); layer = &lt;CALayer: 0xd98c270&gt;&gt; ...
</code></pre>

<p><strong>我的问题是:1)这会导致模糊吗?和 2) 确保 x 和 y 原点都没有最终具有十进制值的最佳方法是什么? (除了手动计算布局)</strong></p>

<p>作为引用,这是我的 UICollectionView 代码:</p>

<pre><code>//subclass UICollectionViewFlowLayout

@implementation LabelLayout

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}

- (id)init
{
    self = ;
    if (self) {
      ;
    }
    return self;
}

-(void)prepareLayout
{
    ;
    _cellCount = [ numberOfItemsInSection:0];
}

- (void)setup
{
    self.itemSize = CGSizeMake(220.0f, 45.0f);
    self.sectionInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
    self.minimumLineSpacing = 10.0f;
    self.minimumInteritemSpacing = 20.0f;
}

@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这肯定会导致模糊,因为所有内容都在两个像素上进行了抗锯齿处理(注意:在视网膜屏幕上,您不会看到任何模糊,因为每个点实际上是两个像素,所以从技术上讲,半个点作为单个像素存在) .要修复它,您可能必须使用 CGRectIntegral 将帧强制为整数边界。最好的方法是:</p>

<pre><code>- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    NSArray *attributes = ;
    for (NSLayoutAttributes *attribute in attributes){
      attribute.frame = CGRectIntegral(attribute.frame);
    }
}
</code></pre>

<p>你应该对 <code>layoutAttributesForItemAtIndexPath:</code> 做同样的事情。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UICollectionView 单元格框架具有十进制原值,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/17957742/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/17957742/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UICollectionView 单元格框架具有十进制原值