当我在 UITextfield 中输入文本时,它是 View subview 的 subview ,整个 View 会向上移动一点。
类是一个 View 。它有两个 subview ,即 collectionView 和具有自己的 subview - UITextfield 的 View 。
我正在 ViewController 中的该类中创建一个对象,并直接在那里设置其约束。其余组件(collectionView 和带有文本字段的 View )的约束在类中设置。
当我使用 panGesture 并将 View 拖动到 UI 动画时,效果很好。但是,当文本输入到文本字段时,布局似乎被重新计算,并且底部约束的值低于所需的值。
有一个 gif 显示了问题的样子: https://gfycat.com/ColorlessTheseAnophelesmosquito
@objc func draggedView(_ sender:UIPanGestureRecognizer) {
ViewController().view.bringSubviewToFront(self)
let translation = sender.translation(in: ViewController().view)
if self.layer.frame.minY >= UIScreen.main.bounds.height * 0.25 && translation.y > 0 {
//values
self.bottomTaskViewConstraint.constant = -30
self.heightTaskViewContraint.constant = 55
//animation
UIView.animate(withDuration: 0.2) {
self.pinPosition = UIScreen.main.bounds.height * 0.33
self.layer.frame.origin.y = self.pinPosition
self.visibleHeight = ViewController().view.frame.maxY - self.frame.maxY - 20
self.bottomConstraint.constant = self.visibleHeight
self.layoutIfNeeded()
}
self.newTaskTextfield.becomeFirstResponder()
} else if self.layer.frame.minY <= UIScreen.main.bounds.height * 0.33 && self.layer.frame.minY > UIScreen.main.bounds.height * 0.25 && translation.y < 0 {
//values
self.bottomTaskViewConstraint.constant = -100
self.heightTaskViewContraint.constant = 110
//animation
UIView.animate(withDuration: 0.2) {
self.pinPosition = UIScreen.main.bounds.height * 0.25
self.layer.frame.origin.y = self.pinPosition
self.visibleHeight = ViewController().view.frame.maxY - self.frame.maxY - 20
self.bottomConstraint.constant = self.visibleHeight
self.layoutIfNeeded()
}
newTaskTextfield.resignFirstResponder()
}
}
最佳答案
我修好了。解决方案: 将 self.view.translatesAutoresizingMaskIntoConstraints = false 添加到 ViewController 中的 viewDidLoad() 中,该 ViewController 是 View 类的父级。
关于swift - 当我在 UITextField 中输入文本时,整个 View 的位置向上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55941742/