我在这里试图迅速解决问题。
我以编程方式通过布局约束进行设计,并且可以正常工作。
结果:
我添加了四个文本字段和四个视图以得到此结果
// label
titre.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
titre.topAnchor.constraint(equalTo: view.topAnchor, constant: 70).isActive = true
titre.widthAnchor.constraint(equalToConstant: 300).isActive = true
titre.heightAnchor.constraint(equalToConstant: 80).isActive = true
//stackview
stackView.topAnchor.constraint(equalTo: box.topAnchor, constant: 0).isActive = true
stackView.leftAnchor.constraint(equalTo: box.leftAnchor, constant: 0).isActive = true
stackView.rightAnchor.constraint(equalTo: box.rightAnchor, constant: 0).isActive = true
stackView.bottomAnchor.constraint(equalTo: box.bottomAnchor, constant: 0).isActive = true
stackView.distribution = .fill
stackView.spacing = 10
// scrollView
box.topAnchor.constraint(equalTo: titre.bottomAnchor, constant: 100).isActive = true
box.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
box.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 40).isActive = true
box.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -40).isActive = true
box.bottomAnchor.constraint(equalTo: startButton.topAnchor, constant: -50).isActive = true
//textefield 1
//joueur1TextField.topAnchor.constraint(equalTo: stackView.topAnchor, constant: 0).isActive = true
joueur1TextField.widthAnchor.constraint(equalTo: box.widthAnchor).isActive = true
joueur1TextField.heightAnchor.constraint(equalToConstant: 40).isActive = true
joueur1TextField.delegate = self
textfields.append(joueur1TextField)
//view1
//view1.topAnchor.constraint(equalTo: joueur1TextField.bottomAnchor, constant: 10).isActive = true
view1.widthAnchor.constraint(equalTo: box.widthAnchor).isActive = true
view1.heightAnchor.constraint(equalToConstant: 2).isActive = true
//textfield2
//joueur2TextField.topAnchor.constraint(equalTo: view1.bottomAnchor, constant: 10).isActive = true
joueur2TextField.widthAnchor.constraint(equalTo: box.widthAnchor).isActive = true
joueur2TextField.heightAnchor.constraint(equalToConstant: 40).isActive = true
joueur2TextField.delegate = self
textfields.append(joueur2TextField)
//view2
//view2.topAnchor.constraint(equalTo: joueur2TextField.bottomAnchor, constant: 10).isActive = true
view2.widthAnchor.constraint(equalTo: box.widthAnchor).isActive = true
view2.heightAnchor.constraint(equalToConstant: 2).isActive = true
//textfield3
//joueur3TextField.topAnchor.constraint(equalTo: view2.bottomAnchor, constant: 10).isActive = true
joueur3TextField.widthAnchor.constraint(equalTo: box.widthAnchor).isActive = true
joueur3TextField.heightAnchor.constraint(equalToConstant: 40).isActive = true
joueur3TextField.delegate = self
textfields.append(joueur3TextField)
//view3
//view3.topAnchor.constraint(equalTo: joueur3TextField.bottomAnchor, constant: 10).isActive = true
view3.widthAnchor.constraint(equalTo: box.widthAnchor).isActive = true
view3.heightAnchor.constraint(equalToConstant: 2).isActive = true
//textfield4
//joueur4TextField.topAnchor.constraint(equalTo: view3.bottomAnchor, constant: 10).isActive = true
joueur4TextField.widthAnchor.constraint(equalTo: box.widthAnchor).isActive = true
joueur4TextField.heightAnchor.constraint(equalToConstant: 40).isActive = true
joueur4TextField.delegate = self
textfields.append(joueur4TextField)
//view4
//view4.topAnchor.constraint(equalTo: joueur4TextField.bottomAnchor, constant: 10).isActive = true
view4.widthAnchor.constraint(equalTo: box.widthAnchor).isActive = true
view4.heightAnchor.constraint(equalToConstant: 2).isActive = true
现在的问题是,当我添加一个新的uitextfield并带有视图时,第一个可以完美地工作,而其他的则存在一些约束条件错误
第一个添加的文本字段:
第二个添加的文本字段:
您可以在此滚动视图中看到视图位置不正确
我的代码添加一个文本框和一个视图以触摸添加按钮:
@objc func ajouterTextField(sender: UIButton){
// incrementation du compteur
index += 1
//creation du uitextfield
let addChamp = UITextField()
addChamp.translatesAutoresizingMaskIntoConstraints = false
addChamp.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
addChamp.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
addChamp.keyboardAppearance = .light
addChamp.attributedPlaceholder = NSAttributedString(string: "joueur\(joueur)", attributes: [NSAttributedStringKey.foregroundColor : #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)])
addChamp.tintColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
//constraints
addChamp.heightAnchor.constraint(equalToConstant: 40).isActive = true
addChamp.delegate = self
stackView.insertArrangedSubview(addChamp, at: index)
textfields.append(addChamp)
//increment index
let indexBarre = index + 1
//creation de la vue
let barre = UIView()
barre.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
barre.alpha = 0.70
barre.translatesAutoresizingMaskIntoConstraints = false
//constraints
barre.heightAnchor.constraint(equalToConstant: 2).isActive = true
stackView.insertArrangedSubview(barre, at: indexBarre)
joueur = joueur + 1
}
感谢您的帮助:)