我正在尝试UICollectionViewCells使用自动布局进行自我调整,但我似乎无法让单元格根据内容调整自己的大小.我无法理解如何根据单元格内容中的内容更新单元格的大小.
这是我尝试过的设置:
UICollectionViewCell一个UITextView.UITextView已禁用.UITextView固定在单元格左侧,显式宽度为320.UITextView固定到单元格的顶部.UITextView具有高度限制设置为其中一个恒定的UITextView报告将适合文本.这是单元格背景设置为红色,UITextView背景设置为蓝色的样子:

我把我已经在GitHub上玩的项目在这里.
我正在使用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) 我在Interface Builder中创建了一个自定义UICollectionViewCell,将它绑定到类上,然后当我想使用并将字符串设置为字符串上的标签时,tha label的值为nil.
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")
}
override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
var cell: LeftMenuCollectionViewCell
cell = collectionView.dequeueReusableCellWithReuseIdentifier("ls", forIndexPath: indexPath) as LeftMenuCollectionViewCell
println(cell.label) // <- this is nil, why??
cell.label.text = "asd"
return cell
}
Run Code Online (Sandbox Code Playgroud)
和子类细胞:
class LeftMenuCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
}
Run Code Online (Sandbox Code Playgroud) 我想加入UICollectionView到ViewController了,我需要有"每行" 3个细胞无细胞之间的空格(它应该看起来像一个网格).单元格宽度应该是屏幕尺寸的三分之一,所以我认为layout.item宽度应该相同.但后来我明白了:

如果我减小那个尺寸(例如7或8像素),那就更好了,但是行中的第三个单元格不是完全可见的,我仍然有那个空白区域(顶部和底部,左侧和右侧).

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var collectionView: UICollectionView?
var screenSize: CGRect!
var screenWidth: CGFloat!
var screenHeight: CGFloat!
override func viewDidLoad() {
super.viewDidLoad()
screenSize = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
// Do any additional setup after loading the view, typically from a nib
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)
layout.itemSize = CGSize(width: screenWidth / 3, height: …Run Code Online (Sandbox Code Playgroud) uicollectionview uicollectionviewcell uicollectionviewlayout swift
我使用的是UICollectionView用UICollectionViewFlowLayout.
我通过设置每个单元格的大小
collectionView:layout:sizeForItemAtIndexPath:
Run Code Online (Sandbox Code Playgroud)
当从纵向切换到横向时,我想调整每个单元格的大小以完全适合CollectionView的大小,而不会在单元格之间留下填充空间.
问题:
1)如何在旋转事件后更改单元格的大小?
2)而且,更好的是,如何使单元格总是适合整个屏幕大小的布局?
我刚开始学习UICollectionViews.我想知道是否有人知道如何指定集合视图中的列数.默认设置为3(iPhone /肖像).我查看了文档,似乎无法找到简明的答案.
所以我已经通过添加第二个视图添加阴影的各种帖子,但如果我想添加它,我仍然无法使其工作UICollectionViewCell.我是子类UICollectionViewCell,这是我的代码,我在单元格的内容视图中添加各种UI元素并向图层添加阴影:
[self.contentView setBackgroundColor:[UIColor whiteColor]];
self.layer.masksToBounds = NO;
self.layer.shadowOffset = CGSizeMake(0, 1);
self.layer.shadowRadius = 1.0;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 0.5;
[self.layer setShadowPath:[[UIBezierPath bezierPathWithRect:self.bounds] CGPath]];
Run Code Online (Sandbox Code Playgroud)
我想知道如何添加圆角和阴影UICollectionViewCell.
我有一个UICollectionView,它从可重用的单元格加载单元格,其中包含标签.数组为该标签提供内容.我可以使用sizeToFit轻松调整标签宽度,具体取决于内容宽度.但我不能让细胞贴合标签.
这是代码
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrayOfStats = @[@"time:",@"2",@"items:",@"10",@"difficulty:",@"hard",@"category:",@"main"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section{
return [arrayOfStats count];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(??????????);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
Cell *cell = (Cell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"qw" forIndexPath:indexPath];
cell.myLabel.text = [NSString stringWithFormat:@"%@",[arrayOfStats objectAtIndex:indexPath.item]];
// make label width depend on text width
[cell.myLabel sizeToFit];
//get the width …Run Code Online (Sandbox Code Playgroud) 我有UICollectionView一个FLowLayout.它会像我期望的那样工作,但是偶尔会有一个单元格无法正确包装.例如,如果实际上在第二行中尾随,则应该在第三行的第一个"列"中打开的单元格,并且它应该是一个空的空间(参见下图).所有你能看到的这个胭脂细胞是左手边(其余的被切掉),它应该是空的.
这不会持续发生; 它并不总是同一行.一旦它发生了,我可以向上滚动然后再回来,单元格将自行修复.或者,当我按下单元格(通过按钮将我带到下一个视图)然后弹回时,我将看到单元格位于错误的位置,然后它将跳转到正确的位置.
滚动速度似乎可以更容易地重现问题.当我慢慢滚动时,我仍然可以偶尔看到单元格处于错误的位置,但是它会立即跳到正确的位置.
当我添加插件插件时,问题就开始了.以前,我的细胞几乎与集合边界齐平(很少或没有插入),我没有注意到这个问题.但这意味着集合视图的右侧和左侧是空的.即,无法滚动.此外,滚动条没有向右冲洗.
我可以在模拟器和iPad 3上解决问题.
我猜这个问题正在发生,因为左右部分插入...但如果值是错误的,那么我希望行为是一致的.我想知道这可能是Apple的错误吗?或许这可能是由于插入物的堆积或类似的东西.

跟进:我一直在使用尼克的这个答案超过2年,现在没有问题(如果人们想知道答案中是否有任何漏洞 - 我还没有找到任何漏洞).做得好尼克.
当我试图把UICollectionCell以UICollectionView在Interface Builder,我不能把它与原因不明.单元格将转到工具栏而不添加UICollectionView
我在用:
ios ×8
objective-c ×3
swift ×3
ios8 ×2
autolayout ×1
ios6 ×1
ios7 ×1
iphone ×1
xcode ×1
xcode6 ×1