iOS(Swift)學習筆記之SnapKit+自定義UI組件

本文爲原創文章,轉載請標明出處html

1. 經過CocoaPods安裝SnapKit

platform :ios, '10.0'

target '<Your Target Name>' do

  use_frameworks!

  pod 'SnapKit', '~> 4.0.0'

end

2. 自定義UI組件

import UIKit
import SnapKit

class CustomView: UIView {

    var isFirstLayout: Bool = true

    lazy var firstView: UIView = {
        let firstView: UIView = UIView()
        return firstView
    }()

    lazy var secondView: UIView = {
        let secondView: UIView = UIView()
        return secondView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.commonInit()
    }

    func commonInit() {
        self.addSubview(self.firstView)
        self.addSubview(self.secondView)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        if self.isFirstLayout {
            self.firstView.snp.makeConstraints { (make) -> Void in

            }

            self.secondView.snp.makeConstraints { (make) -> Void in

            }

            self.isFirstLayout = false
        }
    }
}
相關文章
相關標籤/搜索