AutoLayout Swift demo

import UIKit

import Swift



class ViewController: UIViewController {


override func viewDidLoad() {

super.viewDidLoad()

let v1 = UIView()

let v2 = UIView()

v1.backgroundColor = UIColor.redColor()

v2.backgroundColor = UIColor.blueColor()

//遵循autolayout抛弃原有的宽和高

v1.translatesAutoresizingMaskIntoConstraints = true

v2.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(v1)

view.addSubview(v2)

//item1 =(>=,<=) item2*multiplier + constant

//如果是一元约束的话就是,只针对自己的约束,如果是二元约束的话就必须添加在他们最近的共同父视图上

//set v1's height and width

v1.addConstraint(NSLayoutConstraint(item: v1,attribute: .Width,relatedBy: .Equal,toItem: nil,attribute: .NotAnAttribute,multiplier: 1,constant: 100))//v1 = m*0 + constant

//v1.addConstraint(NSLayoutConstraint(item: v1,attribute: .Height,relatedBy: .Equal,toItem: nil,attribute: .NotAnAttribute,multiplier: 1,constant: 100))

//set relationship between topView and v1

view.addConstraint(NSLayoutConstraint(item: v1,attribute: .Left,toItem: view,attribute: .Leading,constant: 20))

view.addConstraint(NSLayoutConstraint(item: v1,attribute: .CenterY,constant: 0))

//set v2's height and width

view.addConstraint(NSLayoutConstraint(item: v2,toItem: v1,multiplier: 1,constant: 0))

view.addConstraint(NSLayoutConstraint(item: v2,attribute: .Height,36)"> //set relationship between v1 and v2

view.addConstraint(NSLayoutConstraint(item: v2,attribute: .Right,constant: 100))

view.addConstraint(NSLayoutConstraint(item: v1,toItem: v2,constant: 0))

}


override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// dispose of any resources that can be recreated.

}

}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...