Bhavesh
Bhavesh

Reputation: 1422

UIView subview autolayout issue

I have UITableView Cell in which 4 UIViews are arranged in a vertical order like this,

enter image description here

In each UIView, I have two Labels like this,

enter image description here

I have taken a IBOutlet for Height Constraint of all of the Four UIViews and I want to make the height 0 of the view when data is not available in that view. The problem is View is not getting 0 height due to the 10 px bottom constraint of UILabel inside that View.

What i am getting is like this,

enter image description here

Code where constraints are handled

    NSString *toName = [self jointNameFromArray:evidence.arrTo];
    if ([Utility isEmptyString:toName]) {
        cell.toViewHeight.constant = 0;
    }
    else {
        cell.lblToName.text = toName;
    }

    NSString *fromName = [self jointNameFromArray:evidence.arrFrom];
    if ([Utility isEmptyString:fromName]) {
        cell.fromViewHeight.constant = 0;
    }
    else {
        cell.lblFromName.text = fromName;
    }

    NSString *ccName = [self jointNameFromArray:evidence.arrCc];
    if ([Utility isEmptyString:ccName]) {
        cell.ccViewHeight.constant = 0;
    }
    else {
        cell.lblCCName.text = ccName;
    }

    NSString *custName = [self jointNameFromArray:evidence.arrCustodian];
    if ([Utility isEmptyString:custName]) {
        cell.custViewHeight.constant = 0;
    }
    else {
        cell.lblCustName.text = custName;
    }    
     [cell layoutIfNeeded];

Upvotes: 0

Views: 117

Answers (2)

Pushp
Pushp

Reputation: 1060

Set constraint programatically and write logic as u want it is good idea and dont forgot to set layoutifnedded.

Upvotes: 0

bsmith11
bsmith11

Reputation: 296

If you're targeting iOS 9+, I'd suggesting using UIStackView, as it does exactly what you want just by setting the .hidden property of your view to true.

https://www.raizlabs.com/dev/2016/04/uistackview/

If you are unable to use UIStackView, you will need to set one of the vertical padding constraints to a lower priority than the height constraint. You will also need to set .clipsToBounds to true.

Upvotes: 1

Related Questions