2016-05-27 100 views
0

我有兩個標籤,它們排列在另一個之上。這兩者都是在一個UITableViewCell:使用自動佈局根據條件調整標籤位置。

Label arrangement - one on top of another

我想有,做如下聲明:

if indexPath.row == 0 || indexPath.row == 1 { 
    //Remove the bottom label and center the top label vertically in the cell 
} 

if indexPath.row == 2 || indexPath.row == 3 { 
    //Keep both labels one on top of the other 
} 

這些都在一個文件的.xib創建並裝入一個表視圖。我想爲此使用自動佈局,並且我知道我需要使用幾個網點才能實現此功能。任何想法如何做到這一點?

+1

如果你可以針對> = iOS9然後用'UIStackView'此流的標籤。 –

+0

您可以在UILabels的故事板中同時具有主底部約束和centerY約束;然後用IBOutlets將這些約束連接到您的單元類。如果你想啓用/禁用這些約束 – AnthonyR

+0

@MikePollard是正確的,你可以在stackView中設置兩個標籤,以約束爲中心堆棧視圖,並隱藏/取消隱藏第二個標籤 – javiazo

回答

2
func autolayout() 
{ 

    if indexPath.row == 0 || indexPath.row == 1 { 
     //Remove the bottom label and center the top label vertically in the cell 
     bottomLbl.hidden=true 
     self.constrainToplbl_top.constant = 20 

    } 

    if indexPath.row == 2 || indexPath.row == 3 { 
     //Keep both labels one on top of the other 
     self.constrainToplbl_top.constant = 24 
     self.constrainbottomlbl_top.constant = 2 



    } 
     self.layoutIfNeeded() 
} 
0

注意:不要複製的代碼,因爲它可能會導致編譯/語法錯誤

if indexPath.row == 0 || indexPath.row == 1 { 
    //Remove the bottom label and center the top label vertically in the cell 

    cell.lbl2.hidden = true 
    cell.constCenter.constant = -cell.lbl2.frame.size.height/2 
    // cell.constCenter obj of Label 1 center constraint below image display 
} 

if indexPath.row == 2 || indexPath.row == 3 { 
    //Keep both labels one on top of the other 
    cell.lbl2.hidden = false 
    cell.constCenter.constant = 0 
    //by default it is like so no need to do anything 
} 

enter image description hereenter image description here

enter image description here

//請丟棄在屏幕下方拍攝圖像

+0

)嘿,請求的代碼是快速的 –

0

您在單元格中添加3 UILabels(topLbl,centerLbl,bottomLbl),然後隱藏並sh你需要顯示

if indexPath.row == 0 || indexPath.row == 1 { 
    //Remove the bottom label and center the top label vertically in the cell 
    topLbl.hidden=true 
    centerLbl.hidden=false 
    bottomLbl.hidden=true 
} 

if indexPath.row == 2 || indexPath.row == 3 { 
    //Keep both labels one on top of the other 
    topLbl.hidden=false 
    centerLbl.hidden=true 
    bottomLbl.hidden=false 
} 
相關問題