ios - 当 UITextFields 第一响应者更改时,查看 alpha 和 frame 属性损坏
问题描述
我有一个视图(让我们称之为thirdStackView
)和两个textfield
(让我们称之为文本字段b
和c
)。
在我的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
}
}
解决方案
当我将超级视图“translatesAutoresizingMaskIntoConstraints”属性设置为 false 时,它解决了。我认为,当它是真的时,自动布局约束搞砸了。
self.view.translatesAutoresizingMaskIntoConstraints = false
对于这个答案还不够的大师,您可以查看Apple官方文档
https://developer.apple.com/documentation/uikit/uiview/1622572-translatesautoresizingmaskintoco
来自 Apple 文档
请注意,自动调整掩码约束完全指定了视图的大小和位置;因此,您不能添加额外的约束来修改此大小或位置而不引入冲突。
推荐阅读
- node.js - 为什么当我尝试创建数据时会引发此错误?
- python - 列出更高范围的变量
- javascript - 如何实现具有权重比例节点高度的 Highcharts sankey 图
- flutter - 有没有一种方法可以让我们在后台运行应用程序?
- amazon-cognito - 如何在 AWS Cognito 中取消密码重置?
- visual-studio-code - 当焦点在终端上时,有什么方法可以通过键盘快捷键在 VScode 中切换窗口?
- android - 如何在 RecyclerView 的适配器本身中获得以前的位置?
- javascript - 了解这段代码中的回调函数
- ruby-on-rails - 在 Windows 中降级 ruby 版本
- loops - 如何将事件处理程序添加到 Vue 循环中的第一项?