美文网首页
iOS swift-view不能勾选xib了

iOS swift-view不能勾选xib了

作者: 青菜白玉堂 | 来源:发表于2017-07-17 17:42 被阅读120次

    1.先创建一个集成自view的  UseBikeView_one.swift

    2.再创建一个同名xib文件,UseBikeView_one.xib

    3.把xib文件按如下操作

    4.到UseBikeView_one.swift设置如下代码,即可正常使用xib

    var XibView:UIView!

    //初始化时将xib中的view添加进来

    override init(frame: CGRect) {

    super.init(frame: frame)

    XibView = loadViewFromNib()

    print("大小\(frame)")

    XibView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)

    addSubview(XibView)

    }

    //初始化时将xib中的view添加进来

    required init?(coder aDecoder: NSCoder) {

    super.init(coder: aDecoder)

    XibView = loadViewFromNib()

    addSubview(XibView)

    }

    //加载xib

    func loadViewFromNib() -> UIView {

    let className = type(of: self)

    let bundle = Bundle(for: className)

    let name = NSStringFromClass(className).components(separatedBy: ".").last

    let nib = UINib(nibName: name!, bundle: bundle)

    let view = nib.instantiate(withOwner: self, options: nil).first as! UIView

    return view

    }

    相关文章

      网友评论

          本文标题:iOS swift-view不能勾选xib了

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