在viewForHeaderInSection中更改自动布局约束

时间:2015-07-17 10:31:33

标签: swift uitableview autolayout

我有一个自定义节头,并为XIB文件创建了一个自动布局约束的插座,这样我就可以动态更改约束的值。加载视图后,这很有用。但是我在初始加载时无法正确显示视图。出于某种原因,我在viewForHeaderInSection中设置约束的常量值的调用没有被尊重。这使得视图显示的位置与在XIB文件中设置的完全相同,而不是我想要的动态可变方式。这是相关部分(Swift)中的代码。无法正常工作的行是header.spaceAboveBottomView.constant = 0.请帮忙!

class GroupHeaderView: UITableViewHeaderFooterView {
    ....
    @IBOutlet weak var bottomView: DropDownTabView!
    @IBOutlet weak var spaceAboveBottomView: NSLayoutConstraint!
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    var  header = NSBundle.mainBundle().loadNibNamed("GroupHeaderView", owner: self, options: nil).first as! GroupHeaderView

    header.bottomView.setTranslatesAutoresizingMaskIntoConstraints(false)

    if (condition) {
        header.spaceAboveBottomView.constant = 48
    }
    else {
        header.spaceAboveBottomView.constant = 0   // NOT WORKING PROPERLY - XIB FILE HAS THIS CONSTRAINT AS 48, AND THIS CALL TO SET IT TO 0 ISN'T WORKING
    }

    header.bottomView.setNeedsDisplay()    // I'm sure I don't need all of these calls, but I have them just in case
    header.bottomView.layoutIfNeeded()
    header.layoutIfNeeded()
    header.setNeedsLayout()
    header.bottomView.setNeedsLayout()

    return header
}

0 个答案:

没有答案