swift使用NSLayoutConstraint进行控件布局

摘抄部分概念


该函数有如下参数:
第一个参数 view1: 要设置的视图;
第二个参数 attr1: view1要设置的属性,稍后详解;
第三个参数 relation: 视图view1和view2的指定属性之间的关系,稍后详解;
第四个参数 view2: 参照的视图;
第五个参数 attr2: 参照视图view2的属性,稍后详解;
第六个参数 multiplier: 视图view1的指定属性是参照视图view2制定属性的多少倍;
第七个参数: 视图view1的指定属性需要加的浮点数。

Paste_Image.png

代码实例


import UIKit
class ViewController: UIViewController {

        override func viewDidLoad() {
        super.viewDidLoad()
        
        /// 添加ImageView
        let myImageView = UIImageView(frame: CGRect(x: 0 ,  y: 0, width: 80, height: 80))
        myImageView.image = UIImage(named: "xxx")
        myImageView.contentMode = UIViewContentMode.scaleAspectFill
        self.view.addSubview(myImageView)     

        myImageView.translatesAutoresizingMaskIntoConstraints = false  //这里必须要设置成false

        //居中对齐(centerX,centerY属性)
        let myImageViewcentx:NSLayoutConstraint = NSLayoutConstraint(item: myImageView, attribute:  NSLayoutAttribute.centerX, relatedBy:NSLayoutRelation.equal, toItem:self.view, attribute:NSLayoutAttribute.centerX, multiplier:1.0, constant: 0 )
        myImageView.superview!.addConstraint(myImageViewcentx)//父控件添加约束
               
        let myImageViewcenty:NSLayoutConstraint = NSLayoutConstraint(item: myImageView, attribute: NSLayoutAttribute.centerY, relatedBy:NSLayoutRelation.equal, toItem:self.view, attribute:NSLayoutAttribute.centerY, multiplier:1.0, constant:0 )
        myImageView.superview!.addConstraint(myImageViewcenty)//父控件添加约束
        

        //标签示例
        let label1 = UILabel(frame: CGRect(x: 105, y: 93, width: 164, height: 56))
        label1.font = UIFont.boldSystemFont(ofSize: 25)
        label1.textAlignment = NSTextAlignment.center
        label1.textColor = UIColor.white
        //label1.backgroundColor = UIColor.blue
        label1.text = "test"
        self.view.addSubview(label1)

        label1.translatesAutoresizingMaskIntoConstraints = false
        
        //距右边60
        let Label1right:NSLayoutConstraint = NSLayoutConstraint(item: label1, attribute:  NSLayoutAttribute.right, relatedBy:NSLayoutRelation.equal, toItem:self.view, attribute:NSLayoutAttribute.right, multiplier:1.0, constant: -60 )
        label1.superview!.addConstraint(Label1right)//父控件添加约束
        
        //距左边60
        let Label1left:NSLayoutConstraint = NSLayoutConstraint(item: label1, attribute: NSLayoutAttribute.left, relatedBy:NSLayoutRelation.equal, toItem:self.view, attribute:NSLayoutAttribute.left, multiplier:1.0, constant: 60 ) 
        label1.superview!.addConstraint(Label1left)//父控件添加约束
        
        //距上部73
        let Label1top:NSLayoutConstraint = NSLayoutConstraint(item: label1, attribute: NSLayoutAttribute.top , relatedBy:NSLayoutRelation.equal, toItem:self.view, attribute:NSLayoutAttribute.top, multiplier:1.0, constant: 73)     
        label1.superview!.addConstraint(Label1top)

        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


}


实现一个覆盖圆形图案的正方形按钮:

    @IBOutlet weak var uiButton: UIButton!
    let layer = CAShapeLayer()

    override func viewDidLoad() {
        super.viewDidLoad()

        //在界面中间画一个直径100的圆
        self.layer.fillColor = UIColor.white.cgColor
        layer.strokeEnd = 0.4 //设置透明度
        layer.strokeColor = UIColor.gray.cgColor
        self.layer.path = UIBezierPath(arcCenter: CGPoint(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2), radius: 50, startAngle: 0, endAngle: 2.0*CGFloat(M_PI), clockwise: false).cgPath
        self.view.layer.addSublayer(layer)
 
        //设置按钮宽度和高度为100. 
        //宽度约束
        let width:NSLayoutConstraint = NSLayoutConstraint(item: uiButton, attribute: NSLayoutAttribute.width, relatedBy:NSLayoutRelation.equal, toItem:nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier:0.0, constant:100)
        uiButton.addConstraint(width)//自己添加约束
        
        //高度约束
        let height:NSLayoutConstraint = NSLayoutConstraint(item: uiButton, attribute: NSLayoutAttribute.height, relatedBy:NSLayoutRelation.equal, toItem:nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier:0.0, constant:100)
        uiButton.addConstraint(height)//自己添加约束
        
        
      }

实现效果:

Paste_Image.png

控件A顶部,距离 控件B底部的距离:

      let outTexttop:NSLayoutConstraint = NSLayoutConstraint(item: A控件, attribute: NSLayoutAttribute.top , relatedBy:NSLayoutRelation.equal, toItem:B控件 , attribute:NSLayoutAttribute.bottom, multiplier:1.0, constant: 50)
        outText.superview!.addConstraint(outTexttop)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 作为一个移动端的程序员,布局永远都是需要打交道的东西,最初刚入门那会,用的storyboard,xib手动拖拽的方...
    taosiyu阅读 2,646评论 0 4
  • Auto Layout 翻译过来就是自动布局。在ios中Auto Layout 会根据我们在视图上所设置的约束来动...
    遇见0620阅读 13,481评论 1 6
  • 目录 0、前言 一、Auto Layout前世今生 二、Auto Layout基础知识 1.Auto Layout...
    浮游lb阅读 25,476评论 3 90
  • 初识iOS APP开发#### 在iOS APP开发中, main函数仍是程序的入口和出口, 但main函数不需要...
    DeanYan阅读 6,626评论 0 3
  • 越来越多的同学分出来,都已经按着自己的计划行事了 我的成绩在明天下午也即将落幕,一年了,终于是要有个小结了 成绩总...
    我航小天使阅读 265评论 0 0

友情链接更多精彩内容