Reputation: 356
I have a label that is in a custom uitableview cell. I am using auto layout to position the label. My question is how can one set the height of the label to zero when it does not have any text. Or how to to make the label height 0. The project supports ios 8 so using stackview is out of the question. The label is also expansive i can't set a constant height constraint.My layout is given below .
Upvotes: 3
Views: 5012
Reputation: 174
Set the text as nil as and when required and set the content hugging priority high
Upvotes: 0
Reputation: 363
First set Height Constraint to Label. ctrl + drag the height Constraint to class to connect outlet to constraint. i.e
@IBOutlet weak var LabelHeightConstraint: NSLayoutConstraint!
and in implementation set the Height Constraint of Label to zero. As shown below....
self.LabelHeightConstraint.constant = 0;
if(check the label contains data)
{
self.LabelHeightConstraint.constant = 20;
}
else
{
self.LabelHeightConstraint.constant = 0;
}
This Logic will surely work......
Upvotes: 0
Reputation: 1236
In tableviewcell give height constraints except UILabel which one you need to hide.Change your Tableviewcell height it works.
For example, Your UILabel height is '30' and tableviewcell height is 130.You can give 130-30 = 100 in cell height.
//only sample code
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0
return 130
else
return 100
}
Upvotes: 0
Reputation: 27428
Another solution is possible that, set height constraint and take outlet
of it and set it's constant
to zero when text is nil.
Just ctrl + drag from height constraint to class to connect outlet to constraint.
then you can programatically change it's height when needed by changing it's constant.
for example,
@IBOutlet weak var labelHeightConstraint: NSLayoutConstraint!
and change constant like,
self.labelHeightConstraint.constant = 0
Hope this will help :)
Upvotes: 1
Reputation: 1382
You can set label's height constraint >= 0. Label will update it's size to the content, and if text is missing height will be 0.
Upvotes: 5