在具有约束的UIView中移动UITextField

时间:2018-08-13 19:39:34

标签: swift xcode uiview uitextfield constraints

我正在制作一个可以供用户输入其汽车信息的应用程序。我需要UITextFields向上移动,以便它们不会被键盘覆盖。我是用这段代码完成的。

func textFieldDidBeginEditing(_ textField: UITextField)
{
    if make==textField
    {
        self.infoContainer.frame = self.infoContainer.frame.offsetBy(dx: 0, dy:-50)
    }
    if model==textField
    {
        self.infoContainer.frame = self.infoContainer.frame.offsetBy(dx: 0, dy:-75)
    }
    if color==textField
    {
        self.infoContainer.frame = self.infoContainer.frame.offsetBy(dx: 0, dy:-100)
    }
}

在我向UIView * infoContainer添加约束之前,这一直很好。那么它只会工作一次。就像我单击“制作”将其上移-50或单击颜色将其上移-100一样。但是在那之后,它将不再移动。就像如果我单击“制作”将其上移-50,但是如果我单击“颜色”,它将不会上移至-100。因此,然后我尝试通过更改约束来做到这一点。

func textFieldDidBeginEditing(_ textField: UITextField)
    {
        if make==textField
        {
            infoContainer.translatesAutoresizingMaskIntoConstraints = false

            self.view.addSubview(infoContainer)

            let leadingConstraint = infoContainer.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
            let trailingConstraint = infoContainer.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
            let topConstraint = infoContainer.topAnchor.constraint(equalTo: self.view.topAnchor, constant: -50)
            let bottomConstraint = infoContainer.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -50)

            initialConstraints.append(contentsOf: [leadingConstraint,trailingConstraint,topConstraint,bottomConstraint])

            NSLayoutConstraint.activate(initialConstraints)
        }
        if model==textField
        {
            infoContainer.translatesAutoresizingMaskIntoConstraints = false

            self.view.addSubview(infoContainer)

            let leadingConstraint = infoContainer.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
            let trailingConstraint = infoContainer.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
            let topConstraint = infoContainer.topAnchor.constraint(equalTo: self.view.topAnchor, constant: -75)
            let bottomConstraint = infoContainer.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -75)

            initialConstraints.append(contentsOf: [leadingConstraint,trailingConstraint,topConstraint,bottomConstraint])

            NSLayoutConstraint.activate(initialConstraints)
        }
        if color==textField
        {
            infoContainer.translatesAutoresizingMaskIntoConstraints = false

            self.view.addSubview(infoContainer)

            let leadingConstraint = infoContainer.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
            let trailingConstraint = infoContainer.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
            let topConstraint = infoContainer.topAnchor.constraint(equalTo: self.view.topAnchor, constant: -100)
            let bottomConstraint = infoContainer.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -100)

            initialConstraints.append(contentsOf: [leadingConstraint,trailingConstraint,topConstraint,bottomConstraint])

            NSLayoutConstraint.activate(initialConstraints)
        }
    }

效果更好。如果单击“使它上移-50”,然后模型上移-75,然后“颜色”上移-100。但是然后它不会向下移动,就像我单击“使”时一样,它也不会向下移动到-50。我什至将颜色设置为+50。当我单击颜色时,它将下降+50,然后当我单击“制造”时,它将上升到-50,而模型将其上移到-75。但是,如果我再次单击颜色,它将不会移回+50。我要去哪里错了?

1 个答案:

答案 0 :(得分:1)

问题在于,您每次移动都会添加约束,而随着时间的推移会产生冲突,因此您只需添加一次约束,并调整要移动的约束的常数值,而不必这样做

1-在viewDidLoad内添加此代码段

var bottomConstraint:NSLayoutConstraint!

//

infoContainer.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(infoContainer)
let leadingConstraint = infoContainer.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
let trailingConstraint = infoContainer.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
let topConstraint = infoContainer.topAnchor.constraint(equalTo: self.view.topAnchor, constant: -50)
bottomConstraint = infoContainer.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -50)
initialConstraints.append(contentsOf: [leadingConstraint,trailingConstraint,topConstraint,bottomConstraint])
NSLayoutConstraint.activate(initialConstraints)

2-

func textFieldDidBeginEditing(_ textField: UITextField)
{
    if make == textField
    {
        self.bottomConstraint.constant = -50
    }
    if model == textField
    {
        self.bottomConstraint.constant = -75
    }
    if color == textField
    {
        self.bottomConstraint.constant = -100
    }

   self.view.layoutIfNeeded()

   // you can animate it to

   /*
    UIView.animate(withDuration:0.5) {
      self.view.layoutIfNeeded()
    } */
}

如果需要,您可以对topConstraint做同样的操作