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进行的,可是若是可以有一个清晰的视图层次和便于使用的分类方法,即使是很复杂的设计图,也能够很清晰的进行排布,由于注释以及策略选用得当,一者维护起来也并不会困难,两者仍可以适配各类机型,最主要我不须要去写不少的约束代码,以及去备注各个相关约束间的关系; 但自动布局也并非就毫无用武之地,怎么说呢,这个应该是哪种使用更加熟练,哪种就更为适用,固然是以维护和知足设计要求的前提下。