二手轮之 Swift 纯代码布局简化版

前端之家收集整理的这篇文章主要介绍了二手轮之 Swift 纯代码布局简化版前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

二手轮之 Swift 纯代码布局简化版

  • 一、产生原因
  • 二、DEMO装X
  • 三、原理简介
  • 四、如何使用

产生原因

之前做项目时为了节省时间赶工期,经常用storyboard或者XIB来拖拽视图做页面,经常会遇到各种各样的跟代码不兼容的错误,也没有时间静下心来去找寻其中的问题出在哪里,最终还是决定以后用代码布局来做项目了。

现在市面上已经有很多ios的代码布局框架,如Masonry等等。但是对于我等手残党来说,用苹果自带NSLayoutConstraint 就感觉完全够用了。但是原生的在写起来代码又有些复杂,所以就花了些时间对其进行了一次小封装,主要是对 NSLayoutConstraintNSLayoutAttribute 进行了整理。好了话不多少了上代码~

DEMO装X

//先来看看效果(疗效好才有看下去的动力),在使用时,只需要像下面这样写代码就可以:
 label.CenterX@H_404_38@.layout@H_404_38@(constrain: self.view@H_404_38@.CenterX@H_404_38@,constant: 0@H_404_38@)

            .CenterY@H_404_38@.layout@H_404_38@(constrain: self.view@H_404_38@.CenterY@H_404_38@,constant: 0@H_404_38@)

            .Left@H_404_38@.layout@H_404_38@(constrain: self.view@H_404_38@.Left@H_404_38@,constant: 20@H_404_38@)

            .Right@H_404_38@.layout@H_404_38@(constrain: self.view@H_404_38@.Right@H_404_38@,constant: -20@H_404_38@)

            .heightLayoutConstraint@H_404_38@(height: 80@H_404_38@)

完整的DEMO代码如下:

class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad@H_404_38@() let label = UILabel() label.backgroundColor@H_404_38@ = UIColor.blue@H_404_38@ label.text@H_404_38@ = "this is test"@H_404_38@ label.textColor@H_404_38@ = UIColor.yellow@H_404_38@ self.view@H_404_38@.addSubview@H_404_38@(label)//该方法要放到布局代码前 label.CenterX@H_404_38@.layout@H_404_38@(constrain: self.view@H_404_38@.CenterX@H_404_38@,constant: 0@H_404_38@) .CenterY@H_404_38@.layout@H_404_38@(constrain: self.view@H_404_38@.CenterY@H_404_38@,constant: 0@H_404_38@) .Left@H_404_38@.layout@H_404_38@(constrain: self.view@H_404_38@.Left@H_404_38@,constant: 20@H_404_38@) .Right@H_404_38@.layout@H_404_38@(constrain: self.view@H_404_38@.Right@H_404_38@,constant: -20@H_404_38@) .heightLayoutConstraint@H_404_38@(height: 80@H_404_38@) let greenView = UIView() greenView.backgroundColor@H_404_38@ = UIColor.green@H_404_38@ self.view@H_404_38@.addSubview@H_404_38@(greenView) greenView.Top@H_404_38@.layout@H_404_38@(constrain: label.Bottom@H_404_38@,constant: 20@H_404_38@) .CenterX@H_404_38@.layout@H_404_38@(constrain: label.CenterX@H_404_38@,constant: 0@H_404_38@) .widthLayoutConstraint@H_404_38@(width: 80@H_404_38@) .heightLayoutConstraint@H_404_38@(height: 80@H_404_38@) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning@H_404_38@() // Dispose of any resources that can be recreated. } }@H_404_38@

运行起来是这样的:

原理简介:

个人比较喜欢这种链式调用的敲代码方式,当然这个小轮子目前还很不完善,承重能力有限,基本实现思路是通过协议扩展的方式对UIView 进行的功能增强~~将原生的代码布局用到的属性封装成了 ConstraintClass 类。

class@H_404_38@ ConstraintClass@H_404_38@ {@H_404_38@

    var@H_404_38@ view: UIView!

    var@H_404_38@ attribute: NSLayoutAttribute!

    init(view: UIView!,attribute: NSLayoutAttribute!) {



        self@H_404_38@.view = view

        self@H_404_38@.attribute = attribute

    }

    }

然后对NSLayoutAttribute进行了封装,目前就这么简单啦,后面如果有更多的需求和精力的话,再完善功能!!

如何使用:

如果这段简单的代码能入得了看管的法眼,想要拿来试一试的话,欢迎去github上下载 [示例代码] 。(https://github.com/yy910422/YLayoutDemo)。
使用时只需要将DEMO项目中的Layout文件夹拖放到你自己的项目中就可以像示例中的代码那样使用了。另外 UIView+Extensions.swiftUIView 进行了一些属性扩展,可能跟你自己写的有冲突,使用时需要注意。

写在最后:欢迎swift同学交流学习!!站内撕我~~

猜你在找的Swift相关文章