iOS 1个View关联多个xib文件

示例代码,TestView关联2个xib文件,分别是TestView.xibTestView2.xib

1. TestView.xib

1.1 选中File's OwnerClass设置为TestView
TestView.xib.png
1.2 这个View的Class无需设置,默认UIView
TestView.xib.png
1.3 通过拖线的控件都是关联在File's Owner上的
TestView.xib.png

2. TestView2.xib

2.1 选中File's OwnerClass同样设置为TestView
TestView2.xib.png
2.2 同1.2
2.3 将相同控件拖线连接到同一个属性控件上,TestView2.xib中没有subTitleLabel控件,所以没有连线
TestView2.xib.png

3. 代码部分

TestView.swift

import UIKit

class TestView: UIView {
    enum TestType {
        case a
        case b
    }
    
    @IBOutlet var containerView: UIView!
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var subTitleLabel: UILabel!
    
    var type: TestView.TestType = .a
    
    init() {
        super.init(frame: .zero)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(type: TestType) {
        self.init()
        self.type = type
        
        func addXib(_ named: String) {
            Bundle.main.loadNibNamed(named, owner: self, options: nil)
            containerView.frame = self.bounds
            containerView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
            addSubview(containerView)
        }
        
        switch type {
        case .a:
            addXib("TestView")
            
        case .b:
            addXib("TestView2")
        }
    }
    
    var title: String? {
        didSet {
            self.titleLabel.text = title
        }
    }
    
    var subTitle: String? {
        didSet {
            switch type {
            case .a:
                self.subTitleLabel.text = subTitle
            case .b:
                assertionFailure(".b 不支持设置副标题")
            }
        }
    }
}

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let test1 = TestView(type: .a)
        test1.title = "标题"
        test1.subTitle = "副标题"
        self.view.addSubview(test1)
        test1.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            test1.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            test1.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100),
            test1.widthAnchor.constraint(equalToConstant: 100),
            test1.heightAnchor.constraint(greaterThanOrEqualToConstant: 100)
        ])
        
        let test2 = TestView(type: .b)
        test2.title = "标题2"
        self.view.addSubview(test2)
        test2.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            test2.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            test2.topAnchor.constraint(equalTo: test1.bottomAnchor, constant: 20),
            test2.widthAnchor.constraint(equalToConstant: 100),
            test2.heightAnchor.constraint(equalToConstant: 100)
        ])
        
    }
}

效果图:


效果图.png

需要注意的是:由于TestView2.xib中没有subTitleLabel控件,如果test2也设置subTitle的话就会闪退

test2设置subTitle.png

image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容