1

I would like to understand why setting clipsToBounds/masksToBounds to False have no impact on UICollectionViewCell subviews (in my case its UILabel). enter image description here

I tried any possible solution with clipsToBounds/masksToBounds (although I think under the hood they behave/do the same).

override open func layoutSubviews() {
    super.layoutSubviews()
    super.clipsToBounds = false
    super.layer.masksToBounds = false
    self.clipsToBounds = false
    self.layer.masksToBounds = false
    self.contentView.clipsToBounds = false
    self.contentView.layer.masksToBounds = false
    self.contentView.superview?.clipsToBounds = false
    self.contentView.superview?.layer.masksToBounds = false
    self.endTime.layer.masksToBounds = false
    self.endTime.clipsToBounds = false
    self.endTime.layer.zPosition = CGFloat(FLT_MAX)
}

I do override UICollectionViewLayout according to Custom layout from raywenderlich

 override public func prepare() {
    if cache.isEmpty {
        let columnWidth = contentWidth / CGFloat(numberOfColumns)
        var xOffset = [CGFloat]()
        for column in 0 ..< numberOfColumns {
            xOffset.append(CGFloat(column) * columnWidth )
        }
        let column = 0
        var yOffset = [CGFloat](repeating: 0, count: numberOfColumns)

        for item in 0 ..< collectionView!.numberOfItems(inSection: 0) {

            let indexPath = NSIndexPath(row: item, section:0)
            let frame = CGRect(x: xOffset[column], y: yOffset[column], width: cellSize.width, height: cellSize.height)
            let insetFrame = frame.insetBy(dx: cellPaddingX, dy: cellPaddingY)

            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath as IndexPath)
            attributes.frame = insetFrame
            cache.append(attributes)

            contentHeight = max(contentHeight, frame.maxY)
            yOffset[column] = yOffset[column] + cellSize.height + minimumLineSpacing
        }
    }
}

public override var collectionViewContentSize: CGSize {
    return CGSize(width: contentWidth, height: contentHeight)
}

override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    var layoutAttributes = [UICollectionViewLayoutAttributes]()

    for attributes in cache {
        if attributes.frame.intersects(rect) {
            layoutAttributes.append(attributes)
        }
    }
    return layoutAttributes
}

Result:

enter image description here

The red vertical line indicates "Unavailable" its starts from one cell and may finish in another (depends on time duration). From the UI Debug its seems that each next cell always on the top of previous one.(The 21:00 cell is the only one that looks as I want)

Possible Duplicates: A B

Mike.R
  • 2,824
  • 2
  • 26
  • 34
  • Did you check with hierarchy mode? Here my solution (I think) what I understand from your question. Your label height not greater than your `UICollectionViewCell` It may equal to your `UICollectionViewCell`. `ClipToBouns` works when your `UILabel` greater than your `UICollectionViewCell` height. – Mathi Arasan Jun 28 '17 at 14:16
  • @user3589771Iam not sure if I understood you. My label is smaller than my cell (in height and in width).I just want it to be outside the bounds of my cell (what I think work), but than it get covered by next cell. – Mike.R Jun 28 '17 at 14:26
  • Yes, you understood correctly. Your `UICollectionViewCell` height and width greater than your `UILabel`. That's why I asked you to check your Hierarchy height and width of your `UILabel`. You need increase `UILabel` height and width. – Mathi Arasan Jun 28 '17 at 14:32
  • The bottom layout constraint of UILabel is half his height, for instance if I make it 2X higher its stay on its place as should be. as I told in the hierarchy I see the label cover by next cell. – Mike.R Jun 28 '17 at 15:03
  • Your problem is that lower cells are on top in views hierarchy and is overlaping your top cells, you have a lot of space on top of your cells and that space is overlaping your cells, use the Debug View Hierarchy tool in XCode and see what is wrong – Reinier Melian Jun 29 '17 at 06:56
  • @ReinierMelian you are right regarding "lower cells are on top in views hierarchy" BUT they are NOT overlapping , they exactly where they should be.(based on Debug View Hierarchy).The problem that lower cells are on top of view Hierarchy of previous cell as result I don't see the label of previous cell. – Mike.R Jun 29 '17 at 07:51
  • this is exactly what I am trying to say, sorry for my english, You can solve this setting the background color of your cell to `UIColor.clear` – Reinier Melian Jun 29 '17 at 07:58
  • @ReinierMelian you are right, setting the color to clear is the current work around..... (I also can place the label in the top not in the bottom)....but I was looking for a generic solution not work around ,what if one day I would like to change the color :). – Mike.R Jun 29 '17 at 09:24
  • You can change the order in your subViews, I can post an answer of it if you want to – Reinier Melian Jun 29 '17 at 14:07

0 Answers0