前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >swift下autolayout的实现笔记

swift下autolayout的实现笔记

作者头像
练小习
发布2017-12-29 11:50:39
8270
发布2017-12-29 11:50:39
举报
文章被收录于专栏:练小习的专栏练小习的专栏

swift相关的教程还是太少,很多东西都靠自己琢磨。今天研究了一下别人oc实现的autolayout,写篇笔记。

首先是正常的创建元素,为了熟悉实现的方式,我在学习过程中是完全放弃storyboard的。

  1. var v1 = UILabel()
  2. v1.backgroundColor = UIColor.redColor()
  3. v1.text = "v1"
  4. v1.setTranslatesAutoresizingMaskIntoConstraints(false)
  5. //设置在约束布局系统中是否把自动布局转换为约束布局
  6. self.view.addSubview(v1)

然后添加约束

  1. self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
  2. "H:|-1-[v1]-1-|",
  3. options: .DirectionLeadingToTrailing,
  4. metrics: nil,
  5. views: ["v1":v1]))

constraintsWithVisualFormat:参数为NSString型,指定Contsraint的属性,是垂直方向的限定还是水平方向的限定,参数定义一般如下:

H:Expression 表示水平或者垂直(V)方向上相对于SuperView的位置

options:字典类型的值;这里的值一般在系统定义的一个enum里面选取

metrics:nil;一般为nil ,参数类型为NSDictionary,从外部传入

views:就是上面所加入到NSDictionary中的绑定的元素

表达式规则

|: 表示父视图 -: 表示距离 >= :表示视图间距、宽度和高度必须大于或等于某个值 <= :表示视图间距、宽度和高度必须小宇或等于某个值 == :表示视图间距、宽度或者高度必须等于某个值

比如我们要把上面创建的label设置为距离父视图左右都是10,那么表达式就是

  1. "H:|-10-[v1]-10-|"

我们要让他高40,距离父视图顶部为10

  1. "V:|-10-[v1(==40)]"

如果我们再创建V2,V3两个元素,让他们等宽排列在V1的下面

  1. "H:|-1-[v2(v3)]-[v3]-1-|"

完整的代码就是

  1. import UIKit
  2. class ViewController: UIViewController {
  3. override func viewDidLoad() {
  4. super.viewDidLoad()
  5. var v1 = UILabel()
  6. v1.backgroundColor = UIColor.redColor()
  7. v1.setTranslatesAutoresizingMaskIntoConstraints(false)
  8. v1.textAlignment = NSTextAlignment.Center
  9. v1.text = "v1"
  10. self.view.addSubview(v1)
  11. var v2 = UILabel()
  12. v2.backgroundColor = UIColor.redColor();
  13. v2.setTranslatesAutoresizingMaskIntoConstraints(false)
  14. v2.textAlignment = NSTextAlignment.Center
  15. v2.text = "v2"
  16. self.view.addSubview(v2)
  17. var v3 = UILabel()
  18. v3.backgroundColor = UIColor.redColor();
  19. v3.setTranslatesAutoresizingMaskIntoConstraints(false)
  20. v3.textAlignment = NSTextAlignment.Center
  21. v3.text = "v3"
  22. self.view.addSubview(v3)
  23. self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
  24. "H:|-10-[v1]-10-|",
  25. options: .DirectionLeadingToTrailing,
  26. metrics: nil,
  27. views: ["v1":v1]))
  28. self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
  29. "H:|-10-[v2(v3)]-[v3]-10-|",
  30. options: .DirectionLeadingToTrailing,
  31. metrics: nil,
  32. views: ["v2":v2,"v3":v3]))
  33. self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
  34. "V:|-20-[v1(==40)]-10-[v2(==40)]",
  35. options: .DirectionLeadingToTrailing,
  36. metrics: nil,
  37. views: ["v1":v1, "v2":v2,"v3":v3]))
  38. self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
  39. "V:[v1]-10-[v3(==40)]",
  40. options: .DirectionLeadingToTrailing,
  41. metrics: nil,
  42. views: ["v1":v1, "v3":v3]))
  43. }
  44. override func didReceiveMemoryWarning() {
  45. super.didReceiveMemoryWarning()
  46. // Dispose of any resources that can be recreated.
  47. }
  48. }

效果如下图, 无论在什么宽度的设备下面,他们布局保持不变。

bbb
bbb
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档