调整与自动布局中的CollectionView在CollectionViewCell自动图像 [英] Resize images automatically in CollectionViewCell in CollectionView with Auto Layout

查看:168
本文介绍了调整与自动布局中的CollectionView在CollectionViewCell自动图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有水平UICollectionViewFlowLayout一个的CollectionView。

我想实现:

如果一个设备方向为纵向,宽度的UIImageView将是资格赛,以view.width,让高度自动计算(如它通常与自动布局发生)。和同为风景模式。的示例 - 在iPhone上的标准照片应用程序

不幸的是我没有看到如何与的autoLayout实现它。我上设置的UIImageView约束为它的尺寸与细胞相等。不过貌似卖出本身不能被固定到父视图。

在阅读类似的问题看起来像细胞必须以编程方式使用被调整

  FUNC的CollectionView(的CollectionView:UICollectionView,
    布局collectionViewLayout:UICollectionViewLayout,
    sizeForItemAtIndexPath indexPath:NSIndexPath) - > CGSize {返回CGSize(宽度:宽度,高度:高)}

在这里,我坚持,因为我知道屏幕的宽度,但不知道如何动态计算高度。

关于图像高度:

我有我的形象声明如下:

  VAR pageImages = [UIImage的()覆盖FUNC的CollectionView(的CollectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath) -  GT; UICollectionViewCell {
    让细胞:ImageDetailViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,forIndexPath:indexPath)作为! ImageDetailViewCell
    让CURR = indexPath.row
    让imgName =数据[albumNumber] [CURR] [形象]    cell.DetailImageView.image =的UIImage(命名为:imgName!)
    回报细胞
}


解决方案

如果你使用正确的的UIImageView 调整设置(比如,一方面契合/填),那么你只需要单元格的高度设置为你的CollectionView的(你会得到一个指向它作为 ... sizeForItemAtIndexPath ... 方法的参数之一)的高度。你也应该叫 - 事后你的细胞layoutIfNeeded

I have a collectionView with horizontal UICollectionViewFlowLayout.

I am trying to achieve:

If a device orientation is portrait, UIImageView width will be qual to view.width and let the height be calculated automatically (like it usually happens with Auto Layout). And the same for the landscape mode. Example - standard photo app on the Iphone.

Unfortunately i don't see how to achieve it with autoLayout. I set constraints on UIImageView for it to be equal in size to the cell. But looks like the sell itself cannot be pinned to the Parent View.

After reading similar questions looks like cells must be resized programmatically using

func collectionView(collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {



return CGSize(width: width, height: height)

}

And here i am stuck because i know the width of the screen but don't know how to calculate the height dynamically.

About image height:

I have my image declared like this:

var pageImages = [UIImage]()

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: ImageDetailViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ImageDetailViewCell


    let curr = indexPath.row
    let imgName = data[albumNumber][curr]["image"]

    cell.DetailImageView.image = UIImage(named: imgName!)


    return cell
}

解决方案

If you use the proper UIImageView resize setting (say, aspect fit/fill), then you just need to set the cell's height to your collectionView's (you get a pointer to it as one of the ...sizeForItemAtIndexPath... method parameters) height. You also should call the - layoutIfNeeded method on your cell afterwards.

这篇关于调整与自动布局中的CollectionView在CollectionViewCell自动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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