在故事板原型单元格(Xcode 6,iOS 8 SDK)中自动调整UICollectionViewCell contentView框架的问题仅在iOS 7上运行时发生 [英] Autoresizing issue of UICollectionViewCell contentView's frame in Storyboard prototype cell (Xcode 6, iOS 8 SDK) happens when running on iOS 7 only

查看:263
本文介绍了在故事板原型单元格(Xcode 6,iOS 8 SDK)中自动调整UICollectionViewCell contentView框架的问题仅在iOS 7上运行时发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

注意:Purple是 contentView ,Blue是我的UIButton with圆角。

Note: Purple is the contentView, Blue is my UIButton with rounded corner.

然而,在iOS 7上,Cell中的所有子视图突然缩小到(0,0)的帧,50,50)并且永远不再符合我的Autoresizing规则。

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

ObjC:

-(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]
}

这篇关于在故事板原型单元格(Xcode 6,iOS 8 SDK)中自动调整UICollectionViewCell contentView框架的问题仅在iOS 7上运行时发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆