使用Auto Layout中的VFL(Visual format language)--Swift實現
阿新 • • 發佈:2019-01-04
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var aView = UIView() aView.backgroundColor = UIColor.redColor() aView.setTranslatesAutoresizingMaskIntoConstraints(false) self.view .addSubview(aView) //新增約束 self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[aView]-|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["aView":aView])) self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[aView(==50)]|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["aView":aView])) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }