 代码布局(无snapkit应急用)

let labels = UILabel()
labels.backgroundColor = UIColor.blackColor()
labels.textColor = UIColor.whiteColor()
labels.layer.masksToBounds = true
labels.layer.cornerRadius = 5
labels.textAlignment = NSTextAlignment.Center
labels.alpha = 0.5
labels.text = "..."
super.view.addSubview(labels)

        //自动布局
        labels.translatesAutoresizingMaskIntoConstraints = false
        
        //底部约束
        let bottom:NSLayoutConstraint = NSLayoutConstraint(item: labels, attribute: NSLayoutAttribute.Bottom, relatedBy:NSLayoutRelation.Equal, toItem:self.view, attribute:NSLayoutAttribute.Bottom, multiplier:1.0, constant: -30)
        labels.superview!.addConstraint(bottom)//父控件添加约束
        
        //中心对齐
        let center:NSLayoutConstraint = NSLayoutConstraint(item: labels, attribute: NSLayoutAttribute.CenterX, relatedBy:NSLayoutRelation.Equal, toItem:self.view, attribute:NSLayoutAttribute.CenterX, multiplier:1.0, constant: 0)
        labels.superview!.addConstraint(center)//父控件添加约束
        
        //宽度约束
        let width:NSLayoutConstraint = NSLayoutConstraint(item: labels, attribute: NSLayoutAttribute.Width, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:120)
        labels.addConstraint(width)//自己添加约束
      
        //高度约束
        let height:NSLayoutConstraint = NSLayoutConstraint(item: labels, attribute: NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:30)
        labels.addConstraint(height)//自己添加约束
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 7,116评论 0 3
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 5,817评论 0 1
  • (王安石这张画像看起来挺帅的) 王建军 做语文老师,一个很重要的工作就是为学生出练习题。在选题的过程中,看到了一则...
    东营王建军阅读 3,483评论 14 5
  • 今天无聊的翻了翻qq空间,发现当初为你点赞的好友,与你在评论里聊的不亦乐乎的朋友,却变成了一个陌生的网名。他们究竟...
    釉稚0阅读 1,423评论 0 0
  • 多和充满正能量的人相处做朋友,你就会满满的正能量。远离那些总是给你带来负能量的人,你的生活就不会总是充满阴郁。所谓...
    一缕烟火0阅读 962评论 0 0