-2

I'm building an app that can add UILabel(can move with gesture) on UIView.

First, Add a label and move it to another position. Second, Add another label. Then makeConstraints have an effect on first label.

I wanna maintain first label's position How can I do?

@IBAction func touchAddText(_ sender: Any) {
    logp("touchAddText")
    let newLabel = UILabel()
    newLabel.text = "Text"
    newLabel.isUserInteractionEnabled = true
    newLabel.tag = self.labelList.count + 1
    newLabel.addGestureRecognizer(self.setGesture())
    self.labelList.append(newLabel)
    self.containerMain.addSubview(newLabel)
    newLabel.snp.makeConstraints {
        $0.center.equalToSuperview()
    }
}

enter image description here

1 Answers1

-3

Solved. Don't activate constraints, change label's center with CGPoint.

newLabel.sizeToFit()
newLabel.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: self.containerMain.bounds.midY)