之后更改StackView元素的高度

时间:2020-03-02 21:54:32

标签: ios swift uitextfield uistackview

我在StackView内有一个ScrollView,里面有几个TextFields。当该特定文本字段开始编辑时,我想更改textField.height之一。

我在textFieldShouldBeginEditing内尝试过,但是XCode破坏了height.constraint

//delegate Methode für Password Textfield
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    switch textField {
    case passwordTextField:
        eyeButtonOne.isHidden = false
        passwordTextField.addSubview(checkLetterLabel)
        checkLetterLabel.addSubview(checkLetterImage)

        passwordTextField.addSubview(checkNumberLabel)
        checkNumberLabel.addSubview(checkNumberImage)

        passwordTextField.addSubview(checkLengthLabel)
        checkLengthLabel.addSubview(checkLengthImage)

        checkLetterLabel.topAnchor.constraint(equalTo: passwordTextField.bottomAnchor, constant: 5).isActive = true
        checkLetterLabel.leadingAnchor.constraint(equalTo: checkLetterImage.leadingAnchor, constant: 13).isActive = true

        checkLetterImage.leadingAnchor.constraint(equalTo: passwordTextField.leadingAnchor).isActive = true
        checkLetterImage.centerYAnchor.constraint(equalTo: checkLetterLabel.centerYAnchor).isActive = true
        checkLetterImage.heightAnchor.constraint(equalToConstant: 10).isActive = true
        checkLetterImage.widthAnchor.constraint(equalToConstant: 10).isActive = true

        checkNumberLabel.topAnchor.constraint(equalTo: checkLetterLabel.bottomAnchor, constant: 1).isActive = true
        checkNumberLabel.leadingAnchor.constraint(equalTo: checkNumberImage.leadingAnchor, constant: 13).isActive = true

        checkNumberImage.leadingAnchor.constraint(equalTo: passwordTextField.leadingAnchor).isActive = true
        checkNumberImage.centerYAnchor.constraint(equalTo: checkNumberLabel.centerYAnchor).isActive = true
        checkNumberImage.heightAnchor.constraint(equalToConstant: 10).isActive = true
        checkNumberImage.widthAnchor.constraint(equalToConstant: 10).isActive = true

        checkLengthLabel.topAnchor.constraint(equalTo: checkNumberLabel.bottomAnchor, constant: 1).isActive = true
        checkLengthLabel.leadingAnchor.constraint(equalTo: checkLengthImage.leadingAnchor, constant: 13).isActive = true

        checkLengthImage.leadingAnchor.constraint(equalTo: passwordTextField.leadingAnchor).isActive = true
        checkLengthImage.centerYAnchor.constraint(equalTo: checkLengthLabel.centerYAnchor).isActive = true
        checkLengthImage.heightAnchor.constraint(equalToConstant: 10).isActive = true
        checkLengthImage.widthAnchor.constraint(equalToConstant: 10).isActive = true

        passwordTextField.heightAnchor.constraint(equalToConstant: 140).isActive = true
        theStackView.layoutIfNeeded()


        break
    case passwordWiederholenTextField:
        eyeButtonTwo.isHidden = false
        break
    default:
        break
    }

    return true
}

现在,当passwordTextField beginsEditing时,所有项目都将显示,但是高度没有变化... 如您所见,我也正在打电话给layoutIfNeeded(),但是它什么也没做...我也想拥有某种动画,没什么特别的,最后看起来应该很流畅。

有人知道如何解决此问题吗?

1 个答案:

答案 0 :(得分:1)

要打破高度,这意味着这条线还有1个高度限制

 passwordTextField.heightAnchor.constraint(equalToConstant: 140).isActive = true

创建了另一个1,您需要添加一个变量

var heCon:NSLayoutConstraint!


heCon = passwordTextField.heightAnchor.constraint(equalToConstant: 140)
heCon.isActive = true

然后不断播放

heCon.constant = 100
theStackView.layoutIfNeeded()