首页 > 解决方案 > 当 UITextFields 第一响应者更改时,查看 alpha 和 frame 属性损坏

问题描述

我有一个视图(让我们称之为thirdStackView)和两个textfield(让我们称之为文本字段bc)。

在我的viewdidload中,我给他们的锚点并将文本字段的 alpha 设置为 0,将文本字段的 y 帧设置为 -100。轻按一下按钮,我thirdstachview就可以通过动画移出屏幕,并使文本字段可见,并使它们的帧 +100。

我的问题是,当我更改文本字段时,thirdstackview开始在屏幕上看到并且文本字段的位置移回到我放弃的位置viewdidload。此外,当我关闭键盘时,也会出现此问题。

我想这是关于layoutIfneeded()但可能是什么问题?

override func viewDidLoad() {
    thirdStackView.anchor(self.btnStackView.bottomAnchor, left: self.view.leftAnchor, bottom: self.emergenyBtn.topAnchor, right: self.view.rightAnchor, topConstant: 20, leftConstant: 20, bottomConstant: 13, rightConstant: 20, widthConstant: 0, heightConstant: 0)

    self.createTextField(placeHolderText: getLabelText(key: CMSKeys.CMS_TF_TCKNID), titleText: getLabelText(key: CMSKeys.CMS_TF_TCKNID), imageName: "id-icon",leftButtons: [.backDisabled,.forward], rightButtons: [.cancel],textField: idTextField)
    idTextField.tag = 1
    idTextField.keyboardType = UIKeyboardType.numberPad
    idTextField.anchor(self.btnStackView.bottomAnchor, left: self.view.leftAnchor, bottom: nil, right: self.view.rightAnchor, topConstant: 20, leftConstant: 30, bottomConstant: 0, rightConstant: 30, widthConstant: 0, heightConstant: 40)
    idTextField.alpha = 0
    self.createTextField(placeHolderText: getLabelText(key: CMSKeys.CMS_TF_PHONE), titleText: getLabelText(key: CMSKeys.CMS_TF_PHONE), imageName: "phone-icon",leftButtons: [.back,.forward], rightButtons: [.cancel],textField: phoneTextField)
    phoneTextField.keyboardType = UIKeyboardType.numberPad
    phoneTextField.tag = 2
    phoneTextField.anchor(idTextField.bottomAnchor, left: self.view.leftAnchor, bottom: nil, right: self.view.rightAnchor, topConstant: 20, leftConstant: 30, bottomConstant: 0, rightConstant: 30, widthConstant: 0, heightConstant: 40)
    phoneTextField.alpha = 0
}

我的动画功能;

@objc func adminBtnTapped(_ button: UIButton) {
    if(btnTapped){
        UIView.transition(with: button, duration: 0.5, options: .transitionCrossDissolve, animations: {
            self.emergenyBtn.frame.origin.y -= 20
        }, completion: nil)

        UIView.animate(withDuration: 1, animations: {
            self.thirdStackView.frame.origin.y += self.view.bounds.height

            self.bgView.frame.origin.y += self.view.bounds.height
            self.backBtn.frame.origin.x += self.view.bounds.width

            self.idTextField.frame.origin.y += 100
            self.idTextField.alpha = 1.0
            self.phoneTextField.frame.origin.y += 100
            self.phoneTextField.alpha = 1.0
            self.passTextField.frame.origin.y += 100
            self.passTextField.alpha = 1.0
        }) { (_) in
            //self.emergenyBtn.setTitle("Giriş Yap", for: UIControlState.normal)
            self.view.layoutIfNeeded()
        }
        btnTapped = false
    }
}

标签: iosswiftautolayoutuitextfield

解决方案


当我将超级视图“translatesAutoresizingMaskIntoConstraints”属性设置为 false 时,它​​解决了。我认为,当它是真的时,自动布局约束搞砸了。

self.view.translatesAutoresizingMaskIntoConstraints = false

对于这个答案还不够的大师,您可以查看Apple官方文档

https://developer.apple.com/documentation/uikit/uiview/1622572-translatesautoresizingmaskintoco

来自 Apple 文档

请注意,自动调整掩码约束完全指定了视图的大小和位置;因此,您不能添加额外的约束来修改此大小或位置而不引入冲突。


推荐阅读