thk*_*een 158 objective-c uicollectionviewcell ios7 xcode6 ios8
我正在使用Xcode 6 Beta 3,iOS 8 SDK.使用Swift构建目标iOS 7.0.请通过以下屏幕截图逐步查看我的问题.
我在Storyboard中有一个UICollectionView.1原型UICollectionViewCell,中心包含1个标签(无自动调整规则).紫色背景是标记我在运行时由Cell生成的contentView.该视图最终将基于我的UICollectionViewLayoutDelegate正确调整大小,但不会在iOS 7上调整大小.请注意,我使用的是Xcode 6,问题只发生在iOS 7上.
当我在iOS 8上构建应用程序时.一切都很好.
注意:紫色是contentView,蓝色是我的带有圆角的UIButton.

但是,在iOS 7上,Cell中的所有子视图突然缩小到(0,0,50,50)的帧,并且永远不再符合我的自动调整规则.

我认为这是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 **/
Run Code Online (Sandbox Code Playgroud)
Igo*_*uta 169
contentView已损坏.它也可以在awakeFromNib中修复
ObjC:
- (void)awakeFromNib {
[super awakeFromNib];
self.contentView.frame = self.bounds;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
Run Code Online (Sandbox Code Playgroud)
Swift3:
override func awakeFromNib() {
super.awakeFromNib()
self.contentView.frame = self.bounds
self.contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
Run Code Online (Sandbox Code Playgroud)
eco*_*tax 61
我遇到了同样的问题,并向Apple DTS寻求帮助.他们的答复是:
在iOS 7中,单元格的内容视图通过自动调整掩码自行调整大小.在iOS 8中,这已更改,单元格停止使用自动调整蒙版并开始调整layoutSubviews中的内容视图大小.如果在iOS 8中对nib进行编码然后在iOS 7上对其进行解码,则您将拥有一个没有自动调整掩码的内容视图,并且没有其他方法可以自行调整大小.因此,如果您更改了单元格的框架,则内容视图将不会跟随.
要部署回iOS 7的应用程序必须通过调整内容视图本身,添加自动调整掩码或添加约束来解决此问题.
我想这意味着它不是XCode 6中的一个错误,而是iOS 8 SDK和iOS 7 SDK之间的不兼容性,如果你升级到Xcode 6会遇到它,因为它会自动开始使用iOS 8 SDK.
正如我之前评论的那样,丹尼尔普拉曼描述的解决方法对我有用.Igor Palaguta和KoCMoHaBTa描述的那些看起来更简单,并且似乎有理由给予Apple DTS的答案,所以我稍后会尝试.
小智 60
我遇到了同样的问题,并希望Apple能够在下一个Xcode版本中解决这个问题.同时我使用了一种解决方法.在我的UICollectionViewCell子类中,我只是layoutSubviews在尺寸与大小不同的情况下手动覆盖并调整contentView的collectionViewCell大小.
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
KoC*_*BTa 38
另一个解决方案是设置contentView的大小和自动调整掩码,-collectionView:cellForItemAtIndexPath:如下所示:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"CellID";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
// Set contentView's frame and autoresizingMask
cell.contentView.frame = cell.bounds;
cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
// Your custom code goes here
return cell;
}
Run Code Online (Sandbox Code Playgroud)
这也适用于自动布局,因为自动调整大小的蒙版被转换为约束.
小智 6
在Xcode 6.0.1中,用于UICollectionViewCell的contentView对于iOS7设备是破坏的.它也可以通过在awakeFromNib或init方法中向UICollectionViewCell及其contentView添加适当的约束来修复.
UIView *cellContentView = self.contentView;
cellContentView.translatesAutoresizingMaskIntoConstraints = NO;
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[cellContentView]|"
options:0
metrics:0
views:NSDictionaryOfVariableBindings(cellContentView)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[cellContentView]|"
options:0
metrics:0
views:NSDictionaryOfVariableBindings(cellContentView)]];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52227 次 |
| 最近记录: |