Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell awakeFromNib wrong frame size

I have a UITableViewCell with constraints so that the cells layout correctly on iPhone 6 and iPhone 6+.

The cell contents correctly resize correctly. However, I need to draw a sublayer gradient over one of the views inside. In order to do so, I'm using awakeFromNib to draw the sublayer.

AwakeFromNib is giving the size of the frame prior to it autoresizing and therefore the gradient subview isn't the right size.

If I use layoutSubviews, the first time it's called, the size is also wrong and the cell needs to be scrolled before it has the right size; so that method also isn't working

What method should I be using to get the right frame size in order to draw correctly?

like image 537
user1218464 Avatar asked Sep 25 '14 22:09

user1218464


3 Answers

This code worked for me, is written in Swift.

import UIKit
import QuartzCore

class DemoTableViewCell: UITableViewCell {
    @IBOutlet weak var gradientView: UIView!

    private let gl = CAGradientLayer()

    override func awakeFromNib() {
        super.awakeFromNib()

        // set gradient
        let colors: [AnyObject] = [
            UIColor.clearColor().CGColor,
            UIColor(white: 0, alpha: 0.8).CGColor
        ]
        gl.colors = colors
        gradientView.layer.insertSublayer(gl, atIndex: 0)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        // update gradient
        gl.frame = gradientView.bounds
    }
}
like image 187
Noel Avatar answered Nov 13 '22 18:11

Noel


during awakeFromNib the frame layouts do not appear. On your custom Cell you can either do this

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    // Enter Custom Code
}

Or you can override applyLayoutAttributes method like this

-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    // Enter custom Code here

}

applyLayoutAttributes is called before drawRect method but still has the size parameteres of rect.

like image 21
angshuk nag Avatar answered Nov 13 '22 16:11

angshuk nag


@angshuk answer is perfect where if anyone wants to modify UI inside UITableViewCell. Like in my case I was trying to add a different layer on runtime to an UIView, placed inside UITableViewCell using storyboard/autolayout. Tried the same thing inside awakeFromNib() & layoutSubviews() also. Nothing worked.

Here is the same code using Swift. By the way I am using Xcode 7.3.1 with Swift 2.2.

import UIKit

class InviteMainCellClass: UITableViewCell {

@IBOutlet weak var shareCodeLblSuperView: UIView!  //Custom view added using IB/Autolayout

override func awakeFromNib() {
    super.awakeFromNib()
    //shareCodeLblSuperView.addDashedBorderToView(self.shareCodeLblSuperView, viewBorderColor: UIColor.whiteColor(), borderWidth: 1.0) //Not worked
}

override func drawRect(rect: CGRect) {
    super.drawRect(rect)
    shareCodeLblSuperView.addDashedBorderToView(self.shareCodeLblSuperView, viewBorderColor: UIColor.whiteColor(), borderWidth: 1.0) //Worked
}



override func layoutSubviews() {
    super.layoutSubviews()
    //shareCodeLblSuperView.addDashedBorderToView(self.shareCodeLblSuperView, viewBorderColor: UIColor.whiteColor(), borderWidth: 1.0)//Not worked
}

Hope this helped. Thanks.

like image 8
onCompletion Avatar answered Nov 13 '22 18:11

onCompletion