使用SnapKit,发现很难将新约束附加到现有约束

时间:2017-11-24 11:55:07

标签: ios swift xcode

我目前正在使用SnapKit和Swift,使用演练来制作测验应用。我目前正在制定一些约束,并希望在我的约束数组中添加一些新的约束

我收到错误“元组类型的值'()'没有成员answerButtonsConstraints”。

但我不明白为什么。有人可以帮忙。

我尝试使用SnapKits remakeConstraints重新制作约束,但我没有得到我希望的结果。我希望我可以通过简单的语法更改创建正确的结果,但不确定如何格式化它。非常感谢任何帮助。

var answersButtonsConstraints = [

    self.answerButtons[0].snp.makeConstraints { (make) in
        make.leading.equalTo(answerView)
        make.trailing.equalTo(answerButtons[1].snp.leading).offset(MultipleChoiceViewController.buttonPadding)
        make.top.equalTo(answerView.snp.top)
        make.bottom.equalTo(answerButtons[2].snp.top).offset(MultipleChoiceViewController.buttonPadding)
    },

    self.answerButtons[1].snp.makeConstraints { (make) in
        make.trailing.equalTo(answerView)
        make.top.equalTo(answerView)
        make.bottom.equalTo(answerButtons[3]).offset(MultipleChoiceViewController.buttonPadding)
    },

    self.answerButtons[2].snp.makeConstraints { (make) in
        make.leading.equalTo(answerView)
        make.trailing.equalTo(answerButtons[3].snp.leading).offset(MultipleChoiceViewController.buttonPadding)
        make.bottom.equalTo(answerView)
    },

    self.answerButtons[3].snp.makeConstraints { (make) in
        make.trailing.equalTo(answerView)
        make.bottom.equalTo(answerView)
    }]

    for index in 1..<answerButtons.count {

        answersButtonsConstraints.append().self.answerButtons[index].snp.makeConstraints { (make) in
            make.height.equalTo(answerButtons[index-1].snp.height)
            make.width.equalTo(answerButtons[index-1].snp.width)
        }
    }
}

在底部,在那里,它表示answersButtonsConstrains.append()。self.answerButtons ....是我收到错误的地方。

1 个答案:

答案 0 :(得分:2)

你有语法错误:你需要将参数传递给括号内的append方法,而不是.

之后
    for index in 1..<answerButtons.count {
        answersButtonsConstraints.append(self.answerButtons[index].snp.makeConstraints { (make) in
            make.height.equalTo(answerButtons[index-1].snp.height)
            make.width.equalTo(answerButtons[index-1].snp.width)
        }
    })

或者如果你想让它更具可读性,可以为约束创建一个局部变量:

for index in 1..<answerButtons.count {
        let constraint = self.answerButtons[index].snp.makeConstraints { (make) in
        make.height.equalTo(answerButtons[index-1].snp.height)
        make.width.equalTo(answerButtons[index-1].snp.width)
    }
    answersButtonsConstraints.append(constraint)
}