【问题标题】:Collection view cell vertical top alignment集合视图单元格垂直顶部对齐
【发布时间】:2019-07-05 10:16:45
【问题描述】:

我的集合视图(网格格式)单元格高度有问题。网格格式总共包含两列,所以基本上每行有 2 个单元格。我的集合视图单元格高度根据其中的内容而增加,但内容是中心对齐而不是顶部对齐。我想在具有更大高度的行中实现单元格的高度。我怎样才能做到这一点?请指教。

https://drive.google.com/file/d/0B56cE4V6JI-RYmdQT2pIZ0hYQ2phM3Z2YmJNYU1SeXNnYTNN/view?usp=sharing https://drive.google.com/file/d/0B56cE4V6JI-RQUdqVmV4b244cFI5SGd2TnJfbG1tckdQU21Z/view?usp=sharing https://drive.google.com/file/d/0B56cE4V6JI-RYTYxVzJyVUp1clpJTkVqYjN6QXBPeERvVHZR/view?usp=sharing

我在上面添加了一个链接,这是现在的问题。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        let cellsAcross: CGFloat = 2
        let spaceBetweenCells: CGFloat = 10
        let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
        print(dim) // calculating width
        //calculating my content height
        let toppingHeight = sizeOfString(string: PizzaMenuItems[indexPath.row].pizzaToppings, constrainedToWidth: Double(dim))
        let pizzaNameHeight = sizeOfString(string: PizzaMenuItems[indexPath.row].menuName, constrainedToWidth: Double(dim))
        let newHeight = toppingHeight.height + pizzaNameHeight.height + 57
        // to increase my collection view height
        let heightt = (view.frame.width)/2
        let count = self.PizzaMenuItems.count
        if self.PizzaMenuItems.count == 1 {
        }
        else{
            if count % 2 == 0
            { //even Number
                collectionViewC_Height.constant =  heightt  *  CGFloat(self.PizzaMenuItems.count/2) + 57
            }  else
            {  // odd Number
                collectionViewC_Height.constant =  heightt  *  CGFloat(self.PizzaMenuItems.count/2+1)}
        }
        return CGSize(width: dim, height:  newHeight)}

【问题讨论】:

  • 您可以预先计算每个单元格的所有高度,并根据赔率或现在检查第+1行或第-1行,并使用最大值作为单元格的高度?

标签: swift iphone xcode uicollectionview uicollectionviewcell


【解决方案1】:

口头解决方法:

  1. 在您的集合单元 xib 中,不要在容器视图上指定 UILabel。
  2. 在具有 (top:0,lead:0,trail:0,bottom:0) 约束的容器视图的底层放置一个 UIView。
  3. 现在放置带有约束的 UILabel(top:0,lead:0,trail:0,height:
  4. 这有助于您根据内容调整标签高度,从而有助于将标签对齐锚定在顶部。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多