2016-04-28 76 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。