Junior — Middle
Comment diviser l'écran en trois parties égales avec trois View ?
sobes.tech IA
Réponse de l'IA
Pour diviser l'écran en trois parties égales avec trois UIView en iOS, vous pouvez utiliser Auto Layout et définir la même hauteur (ou largeur, si vous divisez horizontalement) pour chacun des trois vues et les disposer l'une sous l'autre (ou côte à côte).
Exemple avec disposition verticale :
let view1 = UIView()
let view2 = UIView()
let view3 = UIView()
// Ajout à la superview
superview.addSubview(view1)
superview.addSubview(view2)
superview.addSubview(view3)
// Désactiver le masque de redimensionnement automatique
view1.translatesAutoresizingMaskIntoConstraints = false
view2.translatesAutoresizingMaskIntoConstraints = false
view3.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
// Lier view1 au bord supérieur de la superview
view1.topAnchor.constraint(equalTo: superview.topAnchor),
view1.leadingAnchor.constraint(equalTo: superview.leadingAnchor),
view1.trailingAnchor.constraint(equalTo: superview.trailingAnchor),
// view2 sous view1
view2.topAnchor.constraint(equalTo: view1.bottomAnchor),
view2.leadingAnchor.constraint(equalTo: superview.leadingAnchor),
view2.trailingAnchor.constraint(equalTo: superview.trailingAnchor),
// view3 sous view2
view3.topAnchor.constraint(equalTo: view2.bottomAnchor),
view3.leadingAnchor.constraint(equalTo: superview.leadingAnchor),
view3.trailingAnchor.constraint(equalTo: superview.trailingAnchor),
view3.bottomAnchor.constraint(equalTo: superview.bottomAnchor),
// Les trois vues ont la même hauteur
view1.heightAnchor.constraint(equalTo: view2.heightAnchor),
view2.heightAnchor.constraint(equalTo: view3.heightAnchor)
])
Ainsi, l'écran sera divisé en trois parties égales en hauteur.