将Storyboard中的添加约束,转为代码,更深的了解UI中的界面布局

首先本文中所有的布局控件都是在UIVIewControll中进行的 ,其中Superview 为self.view, View 为layer

首先谈谈代码 和Storyboard 布局 ,不要偏向于某一种技术,也不要以为只会代码就是大神,真真的大神,从来都不拒绝新的技术,也不会抛弃最基本的知识。所有我们需要将SB布局 和代码相结合 ,在适合的时候使用。纯属个人想法,不喜勿喷,O(∩_∩)O。



1  顶部对齐

// align layer and self.view to top
self.view.addConstraint(NSLayoutConstraint(item: layer, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0));

2 垂直居中

// center layer vertically in self.view
self.view.addConstraint(NSLayoutConstraint(item: layer, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0));

3 底部对齐

// align layer and self.view to bottom
self.view.addConstraint(NSLayoutConstraint(item: layer, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0));

4 左对齐

// align layer and self.view to left
self.view.addConstraint(NSLayoutConstraint(item: layer, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0.0));

5 水平居中

// center layer horizontally in self.view
self.view.addConstraint(NSLayoutConstraint(item: layer, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0));

6 右对齐

// align layer and self.view to right
self.view.addConstraint(NSLayoutConstraint(item: layer, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0.0));

7 顶部与view的距离为0 , 可以根据需要改数值 下面不在赘述

// align layer from the top
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": layer]));

8 左部与view的距离为0

// align layer from the left
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": layer]));

9 右 下

// align layer from the right
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": layer]));
// align layer from the bottom
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": layer]));

10 layer 视图的宽高

// width constraint
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view(==0)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": layer]));

// height constraint
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view(==0)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": layer]));


本文是笔者个人平时学习的一点总结 ,如有错误希望大家指出,共同学习 ,共同进步!

你可能感兴趣的:(将Storyboard中的添加约束,转为代码,更深的了解UI中的界面布局)