iOS xib 封装View控件高级玩法

2021-09-03  本文已影响0人  秋叶红90
截屏2021-09-03 下午6.59.51.png

代码如下


import UIKit

@IBDesignable class MYCustomView: UIView {
    @IBOutlet weak var labText: UILabel!
    @IBInspectable var isLak:Bool = false{
        didSet{
            print("=======")
            if isLak {
                self.labText.text = "==========\(isLak)"
            }else{
                self.labText.text = "===========================================================================================\(isLak)"
            }
            
        }
    }
    
    @IBInspectable var backgroundColor1: UIColor?{
        set{
            self.myView?.backgroundColor = newValue
        }
        get{
            return self.myView?.backgroundColor
        }
    }
    
    
    @IBOutlet var myView: UIView!
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initCommon()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.initCommon()
    }
    
    func initCommon() {
        
        
        let nibName = String(describing: type(of: self))
        let bundle = Bundle.init(for: type(of: self))
        let view = bundle.loadNibNamed(nibName, owner: self, options: nil)!.first as! UIView
        
        self.addSubview(view)
        
        view.frame = self.bounds
        
        view.autoresizingMask = [.flexibleWidth,.flexibleHeight]
//        self.backgroundColor
        print("==== 你好呀")
        
    }
    @IBAction func btnClick(_ sender: Any) {
        self.isLak = !self.isLak
    }
    
}


其他xib 引入

截屏2021-09-03 下午7.01.16.png

高度自适应

上一篇下一篇

猜你喜欢

热点阅读