0

I have created a UIView in IB with tag: 6. I use it in tableView.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    // create background view
    var backgroundView =  cell.contentView.viewWithTag(6) as UIView!
    backgroundView.layer.cornerRadius = 10
    backgroundView.layer.masksToBounds = true

    // create gradient layer
    let gradient : CAGradientLayer = CAGradientLayer()

    // create color array
    let arrayColors: [AnyObject] = [
        UIColor (red: 33/255, green: 33/255, blue: 39/255, alpha: 1).CGColor,
        UIColor (red: 24/255, green: 24/255, blue: 28/255, alpha: 1).CGColor]

    // set gradient frame bounds to match view bounds
    gradient.frame = backgroundView.bounds

    // set gradient's color array
    gradient.colors = arrayColors

    gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradient.locations = [0.1, 0.9]
    gradient.endPoint = CGPoint(x: 1, y: 1)

    // replace base layer with gradient layer
    backgroundView.layer.insertSublayer(gradient, atIndex: 0)

The problem is, while the backgroundView resize to Autolayout, the gradient layer does not, as I would expect per:

    gradient.frame = backgroundView.bounds

I could not find any answers applicable to UIViews in a TableView cell.

Question: What is the correct code to force a layer applied to a UIView in TableView to resize with autolayout?

KML
  • 2,302
  • 5
  • 26
  • 47

2 Answers2

0

I think you can try to subclass UITableViewCell then make CAGradientLayer as its default layer.

Then its size should follow its equivalent view although I do not test it.

  • Note that the table view cell has an existing hierarchy of a background view, a selected background view, a multi selection background view, and a content view. Think twice before making changes to its views and layers. – David Rönnqvist Apr 01 '15 at 12:09
0

This is the answer you are looking for: https://stackoverflow.com/a/4111917/2831015

Subclass UIView and use it as your "backgroundView". By overriding the layer class to a CAGradientLayer then you don't need to add the gradient as a sublayer, and such your gradient will autoResize based on the parent.

Community
  • 1
  • 1
Dover8
  • 607
  • 3
  • 17
  • Thanks, this is another implementation in Swift of custom UIView to achieve the autoresizing: http://stackoverflow.com/questions/29111099/calayer-not-resizing-with-autolayout – KML Apr 01 '15 at 12:09
  • @karlml I tried using layoutSublayersOfLayer which did resize the gradient, but it wasn't happening during device rotation (which is when I needed it recalculated) and was always one phase behind. – Dover8 Apr 02 '15 at 13:24
  • Well use the solution I linked to, I know it works as it is in my current app ;-) – KML Apr 02 '15 at 13:26
  • If you have difficulties, let me know and I will explain – KML Apr 02 '15 at 13:27