zoukankan      html  css  js  c++  java
  • Swift 语法糖then

    then是一个swift初始化库,只有80几行的代码库,确可以让初始化变得很优雅。

    1.使用then初始化AnyObject,这里以初始化控件为例

    lazy var label = UILabel().then({
            $0.text = "label"
            $0.textColor = .blue
        })
    let redView = UIView().then { (make) in
            make.backgroundColor = .red
            make.frame = CGRect(x: 50, y: 50,  100, height: 100)
        }
    let button = UIButton().then ({
                $0.setTitle("点我", for: .normal)
                $0.setTitleColor(.red, for: .normal)
            })

    2.thenSnapKit一起使用的方式

    let button1 = UIButton().then { (make) in
                make.setTitle("登录", for: .normal)
                make.setTitleColor(.black, for: .normal)
                view.addSubview(make)
                
                make.snp.makeConstraints({ (make) in
                    make.top.left.right.equalTo(0)
                    make.height.equalTo(100)
                })
            }
    let button2 = UIButton().then({
            $0.setTitle("登录", for: .normal)
            $0.setTitleColor(.black, for: .normal)
            view.addSubview($0)
            
            $0.snp.makeConstraints({
                $0.top.equalTo(button1.snp.bottom)
                $0.left.right.equalTo(0)
                $0.height.equalTo(50)
            })
          })

    3.其他用法

    let newFrame = oldFrame.with {
              $0.size.width = 200
              $0.size.height = 100
        }
        newFrame.width // 200
        newFrame.height // 100
    UserDefaults.standard.do {
            $0.set("devxoul", forKey: "username")
            $0.set("devxoul@gmail.com", forKey: "email")
            $0.synchronize()
        }
  • 相关阅读:
    各种页的意义
    ecstore Fatal error: Class 'base_request' not found
    viewer.js 视图预览demo
    div在另一个div居中对齐
    文件权限解释rwx
    TPshop各个目录模块介绍
    tpshop linux安装下注意事项
    navicate 远程无法链接linux上mysql数据库问题
    关于破解邮箱的一点心得
    linux开启新端口
  • 原文地址:https://www.cnblogs.com/weiboyuan/p/10551744.html
Copyright © 2011-2022 走看看