[Swift] Un exemple d'utilisation des contraintes

DrakenDraken Membre
avril 2015 modifié dans Objective-C, Swift, C, C++ #1

J'ai donné un exemple d'utilisation des contraintes en Swift à  Ligthman18, sur le forum du MOOC de la Sorbonne. Etrangement ce forum modifie certains caractères. C'est pourquoi je remet le code ici.



class ViewController: UIViewController {

let rouge = UIView()

func positionner() {
let dico:Dictionary = ["rouge":rouge]

// CONTRAINTE VERTICALE POUR FIXER LA HAUTEUR DE LA VUE A 100
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[rouge(100)]",
options:nil,
metrics: nil,
views: dico))

// CONTRAINTE HORIZONTALE POUR FIXER LA LARGEUR DE LA VUE A 200
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[rouge(200)]",
options:nil,
metrics: nil,
views: dico))

// CONTRAINTE VERTICALE POUR PLACER LA VUE A 20 POINTS DU BORD HAUT
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[rouge]",
options:nil,
metrics: nil,
views: dico))

// CONTRAINTE HORIZONTALE POUR PLACER LA VUE A 10 POINTS DU BORD HORIZONTAL GAUCHE
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[rouge]",
options:nil,
metrics: nil,
views: dico))

}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.whiteColor()

rouge.setTranslatesAutoresizingMaskIntoConstraints(false)
rouge.backgroundColor = UIColor.redColor()

self.view.addSubview(rouge)

self.positionner()

}

Mots clés:
Connectez-vous ou Inscrivez-vous pour répondre.