我正在使用 Xcode 6 Beta 3、iOS 8 SDK。使用 Swift 构建目标 iOS 7.0。请使用下面的屏幕截图一步一步地引用我的问题。
我在 Storyboard 中有一个 UICollectionView。 1 个原型(prototype) UICollectionViewCell,其中包含 1 个位于中心的标签(无自动调整大小规则)。紫色背景是为了标记我猜的 Cell 在运行时生成的 contentView。该 View 最终将根据我的 UICollectionViewLayoutDelegate 正确调整大小,但不会在 iOS 7 上。请注意,我使用的是 Xcode 6,问题仅发生在 iOS 7 上。
当我在 iOS 8 上构建应用程序时。一切正常。
注意:紫色是 contentView,蓝色是我的圆角 UIButton。
然而,在 iOS 7 上,Cell 内的所有 subViews 突然收缩到 (0,0,50,50) 的框架,不再符合我的 Autoresizing 规则。
我认为这是 iOS 8 SDK 或 Swift 或 Xcode 中的错误?
更新1:这个问题在官方Xcode 6.0.1中依然存在!最好的解决方法就像 KoCMoHaBTa 在下面建议的那样,通过在单元格的 cellForItem 中设置框架(尽管您必须对您的单元格进行子类化)。事实证明,这是 iOS 8 SDK 和 iOS 7 之间的不兼容问题(请查看下面引自 Apple 的 ecotax 的回答)。
更新 2: 将此代码粘贴到 cellForItem 的开头,一切都会好起来的:
/** 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中修复
对象:
- (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]
}
关于objective-c - 仅在 iOS 7 上运行时,Storyboard 原型(prototype)单元(Xcode 6、iOS 8 SDK)中的 UICollectionViewCell contentView 框架的自动调整大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24750158/