在StoryBoard对UICollectionViewCell 进行Autolayout是遇到的Xcode6.01的BUG

使用Sb对UICollectionViewCell 的内容进行Autolayout约束时候,发现了一个Xcode6.01的BUG,就是你对UICollectionCell约束完了之后,在模拟器上现实的Label是居中,但是真机显示的确实不是居中,后来Google了一下,发现了问题,这是因为使用ios8 SDk编译出来的项目运行在iOS 7引起ContentView大小没有变导致的,解决办法之一:就是在你的定义的UICollectionViewCell 的子类重LayoutSubviews方法,在每次对Cell布局时候进行判对,查看self.contentView.frame.size是否和self.frame.size一致作为ContentView是否被自动布局了,然后在重新修改UICollectionViewCell 的frame.

- (void)layoutSubviews{
    [super layoutSubviews];
    BOOL contentViewIsAutoresized = CGSizeEqualToSize(self.frame.size, self.contentView.frame.size);

    if( !contentViewIsAutoresized) {
        CGRect contentViewFrame = self.contentView.frame;
        contentViewFrame.size = self.frame.size;
        self.contentView.frame = contentViewFrame;
    }
    NSLog(@"%@",NSStringFromCGRect(self.contentView.frame));
}

如果没有自定义的Cell类也可以在

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath加入以下代码

[cell.contentView setFrame:cell.bounds];
[cell.contentView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];

还有一种办法就是Xcode6.1.1选中storyboard 然后选择Show in file inspecter 内把ios8.0 and later 修改成ios7.1也能解决;

另外附上StackOverFlow的原来解决方法:http://stackoverflow.com/questions/24750158/autoresizing-issue-of-uicollectionviewcell-contentviews-frame-in-storyboard-pro

原文地址:https://www.cnblogs.com/zuopeng/p/4240823.html