当UITextFields第一响应者更改时,视图alpha和frame属性变坏

时间:2018-05-18 12:28:23

标签: ios swift autolayout uitextfield

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

在我的viewdidload中,我给出了他们的锚点,并将textfields alpha设置为0,同时将y text of textfields设置为-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
    }
}

1 个答案:

答案 0 :(得分:0)

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

self.view.translatesAutoresizingMaskIntoConstraints = false

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

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

来自Apple文档

  

请注意,自动调整遮罩约束完全指定视图的大小和位置;因此,您无法在不引入冲突的情况下添加其他约束来修改此大小或位置。