使用自动布局根据条件调整标签位置。

时间:2016-05-27 08:11:45

标签: ios swift

我有两个标签,一个在另一个上面排列。这两个都在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文件中创建并加载到表视图中。我想为此使用自动布局,我知道我需要使用几个插座来实现这一点。关于如何做到的任何想法?

3 个答案:

答案 0 :(得分: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()
}

答案 1 :(得分: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 here enter image description here

enter image description here

//请在下面的屏幕截图中丢弃图像

答案 2 :(得分:0)

在单元格中添加3 UILabels(topLbl,centerLbl,bottomLbl),然后隐藏并显示需要显示的标签

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
}
相关问题