【问题标题】:Views alpha and frame property spoil when UITextFields first responder changes当 UITextFields 第一响应者更改时,查看 alpha 和 frame 属性损坏
【发布时间】:2018-05-18 12:28:23
【问题描述】:

我有一个视图(我们称为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
    }
}

【问题讨论】:

  • 你有没有试过clipToBounds = true查看。
  • 我把它给了 thirdStackView 和 superview 但没有任何改变。

标签: ios swift autolayout uitextfield


【解决方案1】:

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

self.view.translatesAutoresizingMaskIntoConstraints = false

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

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

来自 Apple 文档

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-05
    • 2011-09-03
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多