问题描述
我使用的是 Xcode 6 Beta 3、iOS 8 SDK.使用 Swift 构建目标 iOS 7.0.请通过下面的屏幕截图逐步参考我的问题.
I'm using Xcode 6 Beta 3, iOS 8 SDK. Build Target iOS 7.0 using Swift. Please refer to my problem step by step with screenshots below.
我在 Storyboard 中有一个 UICollectionView.1 个原型 UICollectionViewCell,它在中心包含 1 个标签(无自动调整大小规则).我猜紫色背景是标记在运行时由 Cell 生成的 contentView.该视图最终将根据我的 UICollectionViewLayoutDelegate 正确调整大小,但不会在 iOS 7 上进行调整.请注意,我使用的是 Xcode 6,问题仅发生在 iOS 7 上.
I have a UICollectionView in Storyboard. 1 Prototype UICollectionViewCell which contains 1 label in the centre (no autoresizing rule). Purple background was to mark a contentView that is generated in runtime by the Cell I guess. That view will be resized properly base on my UICollectionViewLayoutDelegate eventually, but not on iOS 7. Notice that I'm using Xcode 6 and the problem only happens on iOS 7.
当我在 iOS 8 上构建应用程序时.一切正常.
When I build the app on iOS 8. Everything is okay.
注意:紫色是contentView,蓝色是我的圆角UIButton.
Note: Purple is the contentView, Blue is my UIButton with rounded corner.
然而,在iOS 7上,Cell内的所有子视图突然缩小到(0,0,50,50)的框架,不再符合我的自动调整规则.
However, on iOS 7, all the subViews inside the Cell suddenly shrink to the frame of (0,0,50,50) and never conforms to my Autoresizing rule anymore.
我认为这是 iOS 8 SDK 或 Swift 或 Xcode 中的错误?
I assume this is a bug in iOS 8 SDK or Swift or maybe Xcode?
更新1:这个问题在官方的Xcode 6.0.1中依然存在!最好的解决方法就像下面 KoCMoHaBTa 建议的那样,通过在单元格的 cellForItem 中设置框架(尽管您必须对单元格进行子类化).事实证明,这是 iOS 8 SDK 和 iOS 7 之间的不兼容(查看下面引用自 Apple 的 ecotax 的回答).
Update 1: This problem still exists in the official Xcode 6.0.1 ! The best work around is like what KoCMoHaBTa suggested below by setting the frame in cellForItem of the cell (You have to subclass your cell though). It turned out that this is a incompatibility between iOS 8 SDK and iOS 7 (check ecotax's answer below quoted from Apple).
更新 2:将此代码粘贴到您的 cellForItem 的开头,事情应该没问题:
Update 2:Paste this code at the beginning of your cellForItem and things should be okay:
/** Xcode 6 on iOS 7 hot fix **/
cell.contentView.frame = cell.bounds;
cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
/** End of Xcode 6 on iOS 7 hot fix **/
推荐答案
contentView 已损坏.也可以在awakeFromNib中修复
contentView is broken. It can be also fixed in awakeFromNib
对象:
- (void)awakeFromNib {
[super awakeFromNib];
self.contentView.frame = self.bounds;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
Swift3:
override func awakeFromNib() {
super.awakeFromNib()
self.contentView.frame = self.bounds
self.contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
这篇关于仅在 iOS 7 上运行时,故事板原型单元(Xcode 6、iOS 8 SDK)中 UICollectionViewCell contentView 框架的自动调整大小问题发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!