? 代碼布局(無snapkit應(yīng)急用)

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)//自己添加約束
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • (一)Masonry介紹 Masonry是一個輕量級的布局框架 擁有自己的描述語法 采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動布...
    木易林1閱讀 2,587評論 0 3
  • Masonry是一個輕量級的布局框架,擁有自己的描述語法,采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動布局,簡潔明了并具有高可讀性...
    3dcc6cf93bb5閱讀 1,936評論 0 1
  • (王安石這張畫像看起來挺帥的) 王建軍 做語文老師,一個很重要的工作就是為學(xué)生出練習(xí)題。在選題的過程中,看到了一則...
    東營王建軍閱讀 582評論 14 5
  • 今天無聊的翻了翻qq空間,發(fā)現(xiàn)當(dāng)初為你點贊的好友,與你在評論里聊的不亦樂乎的朋友,卻變成了一個陌生的網(wǎng)名。他們究竟...
    釉稚0閱讀 229評論 0 0
  • 多和充滿正能量的人相處做朋友,你就會滿滿的正能量。遠(yuǎn)離那些總是給你帶來負(fù)能量的人,你的生活就不會總是充滿陰郁。所謂...
    一縷煙火0閱讀 152評論 0 0

友情鏈接更多精彩內(nèi)容