要求 宽高200的view,通过代码,使得view在距离父控件的右下角20边距处

/* 约束的设置,控件内部约束由自己添加,比如宽高,如果是与其他的

控件约束那么有父控件添加

*创建约束 NSLayoutConstraint  参数 说明:

* item 自己

* attribute

* relatedBy 大于等于 小于等于 等于

* toItem 另外一个控件

* attribute 另一个控件的熟悉

* multiplier 乘以多少

* constant : 加上多少

* NSLayoutConstraint : 某个控件的属性值 等于 另外一个控件的属性值

乘以多少 加上多少

* 添加约束 addConstraint

*/

swift 代码:

let blueView =UIView();

blueView.backgroundColor =UIColor.blueColor()

self.view.addSubview(blueView)//系统默认会给autoresizing
约束

// 关闭autoresizing 不关闭否则程序崩溃

blueView.translatesAutoresizingMaskIntoConstraints =false

//宽度约束

let width:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Width, relatedBy:NSLayoutRelation.Equal, toItem:nil,
attribute: NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:200)

blueView.addConstraint(width)//自己添加约束

//高度约束

let height:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil,
attribute: NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:200)

blueView.addConstraint(height)//自己添加约束

//右边约束

let right:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Right, relatedBy:NSLayoutRelation.Equal, toItem:self.view,
attribute:NSLayoutAttribute.Right, multiplier:1.0, constant: -20)

blueView.superview!.addConstraint(right)//父控件添加约束

//下边约束

let bottom:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Bottom, relatedBy:NSLayoutRelation.Equal, toItem:self.view,
attribute:NSLayoutAttribute.Bottom, multiplier:1.0, constant: -20)

blueView.superview!.addConstraint(bottom)//父控件添加约束

效果图:

Swift开发:NSLayoutConstraint纯代码实现自动布局-初级篇的更多相关文章

  1. 【好程序员笔记分享】——iOS开发之纯代码键盘退出

    -iOS培训,iOS学习-------型技术博客.期待与您交流!------------ iOS开发之纯代码键盘退出(非常简单)     iOS开发之纯代码键盘退出 前面说到了好几次关于键盘退出的,但 ...

  2. Swift -Login(MVC 纯代码)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #000000 } span.s1 { } span.s2 { c ...

  3. iOS开发学习--纯代码 UIScrollView 无限循环的实现——代码类封装

    一个简单的利用UIScrollView 实现的无线滚动banner,下面的代码实现,因为封装问题,对两个及一下的view 支持出了一点问题(view是传参进来的,不可以生成两份),但是原理是正确的,智 ...

  4. IOS开发之纯代码界面--基本控件使用篇 ┊

    http://www.cocoachina.com/bbs/read.php?tid=131516

  5. iOS UITableViewCell UITableVIewController 纯代码开发

    iOS UITableViewCell UITableVIewController 纯代码开发 <原创> .纯代码 自定义UITableViewCell 直接上代码 ////// #imp ...

  6. 《Swift开发指南》

    <Swift开发指南> 基本信息 作者: 关东升    赵志荣 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115366245 上架时间:2014-8-5 出版日期:20 ...

  7. 【iOS开发】多屏尺的自动适配 AutoLayout (纯代码方式)

    关于AutoLayout,最早从iOS6开始引入使用.   主要功能是使用约束,对视图进行相对布局,以适应不同屏尺的变换.   网上大量的资料都在介绍xib和storyboard,如何使用AutoLa ...

  8. iOS开发——OC篇&纯代码退出键盘

    关于iOS开发中键盘的退出,其实方法有很多中,而且笔者也也学会了不少,包括各种非纯代码界面的退出. 但是最近开始着手项目的时候却闷了,因为太多了,笔者确实知道有很多中方法能实现,而且令我影响最深的就是 ...

  9. swift 之 纯代码创建 cell

    初学swift 但是网上只有很多swift用xib创建的cell,就算是有也不是我想要的.今天自己弄了一个不用xib纯代码写的,来上代码 博客地址: https://github.com/liguol ...

随机推荐

  1. Xcode报错 - 1

    1. xcode在真机调试的时候出现"The identity used to sign the executable is no longer valid" 解析: 是由于.pr ...

  2. Pentaho Data Integration Step: BD Procedure Call

    官网连接:http://wiki.pentaho.com/display/EAI/Call+DB+Procedure 描述 调用数据库存储过程步骤允许用户执行一个数据库存储过程,并且得到结果.存储过程 ...

  3. Js处理json数据

    js中处理由ajax调用返回的json数据问题,可以通过使用JSON.parse方法将json字符串转化成javascript 对象.通过对象访问属性值. JSON.parse 只限于高版本的浏览器. ...

  4. UITableView.m:8042 crash 崩溃

     CRASH : /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:804 ...

  5. 读取spring配置文件的方法(spring读取资源文件)

    1.spring配置文件 <bean id="configproperties" class="org.springframework.beans.factory. ...

  6. c#生成注册码的两种方法(mac地址与IP地址)

    using System; using System.Management; using System.Security.Cryptography; using System.IO; using Sy ...

  7. POJ_2385_Apple_Catching_(动态规划)

    描述 http://poj.org/problem?id=2385 两棵苹果树,给定一个时间t,1~t每分钟有一棵树掉苹果,牛起始在#1树,最多换w次位置,问最多接到多少苹果. Apple Catch ...

  8. xcode5.1 armv7 armv7s arm64 类型, 区分, 概念等

    官方: https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/In ...

  9. Web三维技术:Flash Builder+away3d平台搭建(含演示视频)

    转自:http://www.cnblogs.com/beer/archive/2011/07/08/2101492.html 前言:作为页面中实验设备的显示层,需要一个swf作为显示的UI.虽然可以用 ...

  10. (转载)AS3中的mouseEnabled与mouseChildren

    (转载)http://www.cnblogs.com/gongchen/archive/2013/05/09/3069055.html mouseEnabled与mouseChildren都是用来确定 ...