Swift使用代码进行约束

前端之家收集整理的这篇文章主要介绍了Swift使用代码进行约束前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

版本为Swift2.3

  1. let imgCoupon = UIImageView()
  2. imgCoupon.frame = CGRectMake(0,0,16,16)
  3. imgCoupon.loadImgByNamed("coupon")
  4. imgCoupon.tag = 12345
  5. cell.addSubview(imgCoupon)
  6. imgCoupon.translatesAutoresizingMaskIntoConstraints = false
  7. let l = NSLayoutConstraint(item: imgCoupon,attribute: .Leading,relatedBy: .Equal,toItem: firstView,attribute: .Trailing,multiplier: 1,constant: 3)
  8. l.active = true
  9. let c = NSLayoutConstraint(item: imgCoupon,attribute: .CenterY,toItem: lbName,constant: 0)
  10. c.active = true
  11. let h = NSLayoutConstraint(item: imgCoupon,attribute: .Height,toItem: nil,attribute: .NotAnAttribute,constant: 16)
  12. h.active = true
  13.  
  14. let w = NSLayoutConstraint(item: imgCoupon,attribute: .Width,constant: 16)
  15. w.active = true

Swfit2.3中使用w.active = true 而Swift3.0中是addConstraint(w),并且attribute的值也不同

猜你在找的Swift相关文章