AutoLayout Swift demo

AutoLayout Swift demo,第1张

概述import UIKit import Swift class ViewController: UIViewController {     override func viewDidLoad() {         super.viewDidLoad()         let v1 = UIView()         let v2 = UIView()         v1.backgrou

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.

}

}

总结

以上是内存溢出为你收集整理的AutoLayout Swift demo全部内容,希望文章能够帮你解决AutoLayout Swift demo所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1086882.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存