gooberboobbutt
gooberboobbutt

Reputation: 787

Resizing a label in a custom cell in code

I have a label in a custom cell and it won't resize. Here is the code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {



    let myCell2 = tableView.dequeueReusableCellWithIdentifier("privatecell1", forIndexPath: indexPath) as! YourPrivateControllerCell

    myCell2.barfront1.frame.size.width = 200
    myCell2.barfront1.layer.masksToBounds = true
    return myCell2
}

I know I can set the width by adding a constraint but the label will be a different size for every row. The code works for a normal view controll view but doesn't seem to work for a tableview cell/row. The actual code will be:

myCell2.barfront1.frame.size.width = myCell2.barback1.frame.size.width * percent

but I cant even get the label to resize to 200.

Upvotes: 0

Views: 43

Answers (2)

gooberboobbutt
gooberboobbutt

Reputation: 787

I got it to work. Instead of setting a width for the label I added a layout width constraint. Code:

myCell2.barfront1.addConstraint(NSLayoutConstraint(item: myCell2.barfront1, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: myCell2.barback1.frame.size.width * barwidth1[indexPath.row] * 0.01))

Now the label is a different size for every row.

Upvotes: 0

Jason Nam
Jason Nam

Reputation: 2011

You can use constraints and connect the constraints with @IBOutlet like this. Of course you have to put this in the YourPrivateControllerCell.

@IBOutlet weak var labelWidth: NSLayoutConstraint? = nil

Then call the following method will be change the width of you label.

labelWidth.constant = 200

Good Luck.

Upvotes: 1

Related Questions