之后更改 StackView 元素的高度

Posted

技术标签:

【中文标题】之后更改 StackView 元素的高度【英文标题】:Change height of StackView element afterwards 【发布时间】:2020-03-02 21:54:32 【问题描述】:

我在ScrollView 里面有一个StackView,在里面我有几个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】:

为了打破高度,这意味着这条线还有另外 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()

【讨论】:

我不能打电话给heCon.passwordTextField... ''NSLayoutConstraint' 类型的值没有成员'passwordTextField'

以上是关于之后更改 StackView 元素的高度的主要内容,如果未能解决你的问题,请参考以下文章

水平stackview标签随着固定高度元素增长

如何以编程方式更改 stackView 内单个 UIView 的高度?

Scrollview中的两个动态Stack View

StackView 高度 Swift

Stack View 大小变化无法可视化

如何在 iOS 应用中使滚动视图高度动态化?