xib创建自定义view

转载请注明出处!!!

1.oc版

+ (instancetype)viewFromXib {
    NSBundle* bundle = [NSBundle mainBundle];
    NSString* className = NSStringFromClass([self class]);
    return [[bundle loadNibNamed:className owner:nil options:nil] objectAtIndexSafe:0];
}
使用:
CustomView* view = [CustomView viewFromXib];

2.swift版

static func newInstance() -> LeftView? {
  let objc = Bundle.main.loadNibNamed("LeftView", owner: nil, options: nil)
  if let view = objc?.first as? LeftView {
    return view
  }
  return nil
}

使用:
let view = CustomView.newInstance()!
原文地址:https://www.cnblogs.com/weicyNo-1/p/7509052.html