简体   繁体   中英

UIView subview autolayout issue

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

在此处输入图片说明

In each UIView, I have two Labels like this,

在此处输入图片说明

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,

在此处输入图片说明

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];

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 .

以编程方式设置约束并根据需要编写逻辑,这是个好主意,不要忘记设置layoutifnedded。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM