self.view.backgroundColor = UIColor.greenColor() let superView = UIView(frame: CGRect(x: 20, y: 100, width: 300, height: 400)) superView.backgroundColor = UIColor.blueColor() self.view.addSubview(superView) let label_01 = UILabel() label_01.text = "label_01" label_01.backgroundColor = UIColor.redColor() superView.addSubview(label_01) let label_02 = UILabel() label_02.text = "label_02" label_02.backgroundColor = UIColor.grayColor() superView.addSubview(label_02) let label_03 = UILabel() label_03.text = "label_03" label_03.backgroundColor = UIColor.brownColor() superView.addSubview(label_03) let label_04 = UILabel() label_04.text = "label_04" label_04.backgroundColor = UIColor.cyanColor() superView.addSubview(label_04) label_01.translatesAutoresizingMaskIntoConstraints = false label_02.translatesAutoresizingMaskIntoConstraints = false label_03.translatesAutoresizingMaskIntoConstraints = false label_04.translatesAutoresizingMaskIntoConstraints = false //當屏寬大於兩個按鈕的寬度時,兩個按鈕的間隔的優先級會下降; //使用AlignAllLeft會提示Options mask required views to be aligned on a horizontal edge, which is not allowed for layout that is also horizontal.; 也就是說當我寫水平方向時的佈局時,其實只能使用水平相關的NSLayoutFormatOptions限定,不然會由於VFL語句解析出錯; //設置了左右邊距、間隔與寬度;則左右邊距和寬度優先; let hVFL = "H:|-10-[label_01(60)]-40-[label_02]-30-|" let hCons = NSLayoutConstraint.constraintsWithVisualFormat(hVFL, options: NSLayoutFormatOptions.AlignAllTop, metrics: nil, views: ["label_01":label_01, "label_02":label_02]) superView.addConstraints(hCons) let vVFL1 = "V:|-80-[label_01]" let vCons1 = NSLayoutConstraint.constraintsWithVisualFormat(vVFL1, options: NSLayoutFormatOptions.AlignAllTop, metrics: nil, views: ["label_01":label_01, "label_02":label_02]) superView.addConstraints(vCons1) //水平方向若是指定了上邊距與下邊距 和高度 則以上邊距和高度爲優先; //水平方向只是簡單居中 let hVFL2 = "H:|-[label_03]-|" let hCons2 = NSLayoutConstraint.constraintsWithVisualFormat(hVFL2, options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["label_03":label_03]) superView.addConstraints(hCons2) let hVFL3 = "H:|-[label_04(==label_03)]-|" let hCons3 = NSLayoutConstraint.constraintsWithVisualFormat(hVFL3, options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["label_03":label_03, "label_04":label_04]) superView.addConstraints(hCons3) let vVFL3 = "V:|[label_03(>=60@900)]-50-[label_04(>=400@899)]-30@901-|" let vCons3 = NSLayoutConstraint.constraintsWithVisualFormat(vVFL3, options: NSLayoutFormatOptions.AlignAllLeft, metrics: nil, views: ["label_03":label_03, "label_04":label_04]) superView.addConstraints(vCons3)
______________________________________________________________________________________________________________
12.15補充:
其實若是一個佈局中使用VFL,不可避免的整個佈局約束的語句甚至比佈局還要多,由於要對不一樣的佈局關係進行處理和限制,而涉及到動態修改就會更加麻煩;在個人項目中,不多再用到VFL了,可是有些場景不得不說VFL更方便;現在,個人作法是擴展了UIView的方法,用一些基本的方法作佈局,雖然理論上仍是使用frame進行的,可是若是可以有一個清晰的視圖層次和便於使用的分類方法,即使是很複雜的設計圖,也能夠很清晰的進行排布,由於註釋以及策略選用得當,一者維護起來也並不會困難,兩者仍可以適配各類機型,最主要我不須要去寫不少的約束代碼,以及去備註各個相關約束間的關係; 但自動佈局也並非就毫無用武之地,怎麼說呢,這個應該是哪種使用更加熟練,哪種就更爲適用,固然是以維護和知足設計要求的前提下。