swift学习之路

swift - 使用协议实现view的nib加载

2017-06-08  本文已影响0人  那就这样吧_
import UIKit

protocol NibLoadable {

}

extension NibLoadable where Self : UIView {
    static func loadFromNib(_ nibname : String? = nil) -> Self {
        let loadName = nibname == nil ? "\(self)" : nibname!
        return Bundle.main.loadNibNamed(loadName, owner: nil, options: nil)?.first as! Self
    }
}
import UIKit

class DemoView: UIView, NibLoadable {
    var name : String = ""
}
let demoView = DemoView.loadFromNib()
或
let demoView = DemoView.loadFromNib("DemoView")
上一篇下一篇

猜你喜欢

热点阅读