使用自动布局

时间:2015-05-26 12:08:47

标签: ios swift autolayout uicollectionview

我有一个带有水平UICollectionViewFlowLayout的collectionView。

Here is the layout:

我正在努力实现:

如果设备方向是纵向,则UIImageView宽度将适合view.width并自动计算高度(就像通常在自动布局中一样)。对于横向模式也一样。 示例 - Iphone上的标准照片应用程序。

不幸的是,我不知道如何使用autoLayout实现它。我在UIImageView上设置约束,使其大小与单元格相同。但看起来销售本身不能固定在父视图上。

在阅读类似问题后,看起来必须使用

以编程方式调整单元格大小
func collectionView(collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {



return CGSize(width: width, height: height)

}

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

关于图片高度:

我的图像声明如下:

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
}

2 个答案:

答案 0 :(得分:4)

如果您使用正确的<?php ob_start(); if(isset($_POST['searchstring'])){ include ("connect.php"); $queried = $_POST['searchstring']; $queried = trim($queried); $patterns = array("/\s+/", "/\s([?.!])/"); $replacer = array("+","$1"); $queried = preg_replace( $patterns, $replacer, $queried ); header("Location: index.php?page=search&q=".$queried.""); } else { header('Location: index.php'); } exit(); ?> 调整大小设置(例如,宽高比适合/填充),那么您只需要将单元格的高度设置为您的collectionView(您可以获得指向它的指针{{1方法参数)高度。您之后也应该在您的手机上调用<?php header('Location: index.php'); ?> 方法。

答案 1 :(得分:2)

您可以使用sizeForItemAtIndexPath:更改集合视图单元格的大小。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    var numberOfCellInRow : Int = 3
    var padding : Int = 5
    var collectionCellWidth : CGFloat = (self.view.frame.size.width/CGFloat(numberOfCellInRow)) - CGFloat(padding)
    return CGSize(width: collectionCellWidth , height: collectionCellWidth)
}

您可以通过以下方式获取单元格的大小:

((UICollectionViewFlowLayout) self.collectionViewName).itemSize.height)

您可以通过以下方式获取图片尺寸:

let sizeOfImage = image.size
let height = image.height

如果您想更改高度,请按return CGSize(width: collectionCellWidth , height: cellheight)

手动更改
相关问题