标签:表示 控件 visual tco center gray 示例 像素 水平
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)
标签:表示 控件 visual tco center gray 示例 像素 水平
原文地址:http://www.cnblogs.com/wenKnowledge/p/6045353.html