美文网首页
iOS xib 封装View控件高级玩法

iOS xib 封装View控件高级玩法

作者: 秋叶红90 | 来源:发表于2021-09-03 19:02 被阅读0次
    截屏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

    高度自适应

    相关文章

      网友评论

          本文标题:iOS xib 封装View控件高级玩法

          本文链接:https://www.haomeiwen.com/subject/uowkwltx.html