2016-04-28 73 views
0

我的UITableView电池,其中4个UIViews布置在垂直顺序这样,的UIView子视图自动布局问题

enter image description here

在每UIView的,我有两个标签这样的,

enter image description here

我已经为所有四个UIViews的高度约束采取了一个IBOutlet,并且当数据在该视图中不可用时,我想使视图的高度为0。问题是视图没有得到0高度,因为该视图内的UILabel的10px底部约束。

什么,我得到的是这样的,

enter image description here

代码,其中约束处理

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

你也想隐藏To标签吗? –

+0

@TejaNandamuri当您更改约束条件时需要调用layoutIfNeeded方法时,可以使用 – Bhavesh

+0

。 –

回答

1

如果你的目标iOS的9+,我会用UIStackView提示,因为它只是通过将您的视图的.hidden属性设置为true而完全符合您的要求。

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

如果您无法使用UIStackView,你将需要设置的垂直填充制约因素之一比高度约束较低的优先级。您还需要将.clipsToBounds设置为true

0

以编程方式设置约束并编写逻辑,因为您希望它是个好主意,并且不要忘记设置layoutifnedded。