程序化Autolayout改变了UIView的行为

时间:2016-02-11 09:28:49

标签: ios uiview autolayout

我有一个控制器,我以编程方式添加子视图。通过子视图的配置,我以编程方式添加自动布局约束。 Everthing正在工作,但如果我添加约束,甚至没有显示设置的backgroundcolor,视图也不会对触摸做出反应。

buttonView应显示在我父视图的右下角。

任何想法可能出错?

以下是我添加约束的方法:

 private func configureAutolayoutConstraints(buttonView: UIView, parentView: UIView){

        buttonView.translatesAutoresizingMaskIntoConstraints = false

        let bottomConstraint = NSLayoutConstraint(item: buttonView, attribute:
            .Bottom, relatedBy: .Equal, toItem: parentView, attribute: .Bottom, multiplier: 1.0, constant: -130)

        parentView.addConstraint(bottomConstraint)


        let trailingConstraint = NSLayoutConstraint(item: buttonView, attribute:
            .Trailing, relatedBy: .Equal, toItem: parentView, attribute: .Trailing, multiplier: 1.0, constant: -90)
        parentView.addConstraint(trailingConstraint)

    }

1 个答案:

答案 0 :(得分:1)

Autolayout引擎需要至少4个约束来确定视图。您仅应用了底部和尾部约束。您需要function AddRow() { //The current count of the rows (stored in html as hidden) var rowCount = parseInt(document.getElementById("rowCount").value); var NextRowIndex= rowCount+1; //updating the rowcount value in html page document.getElementById('rowCount').value = NextRowIndex; //Ajax function which retuns a the required row $.ajax({ //requesting to get the required row as response url:urlprefix+"add_row/"+NextRowIndex, beforeSend: function(){ enableLoader(); }, success: function(data) { $("#addFileRow"+rowCount).after("<div id='addFileRow"+NextRowIndex+"' class='sec-container' style='padding-bottom: 18px'>"+ "</div>"); disableLoader(); $("#addFileRow"+NextRowIndex).append(data); }, error: function(data) { alert(data); } }); }; OR width+height约束才能使其正常工作。