problem loading view from nib file ,width of the view is 0.0

67 Views Asked by At

I'm trying to load nib file to view inside collectionViewController as parallax Header, but after loading the view the width of the view is 0.0 and it's not presenting.If I load any other view the width is ok but the width of this view changes to zero after loading.here is my code.

class CastHeader: UIView{
private func commonInit() {
     guard let view = Bundle.main.loadNibNamed("CastHeader", owner: self, options: nil)![0] as? UIView else{return}
        addSubview(view)
        view.translatesAutoresizingMaskIntoConstraints = false
        let bindings = ["view": view]
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options:[], metrics:nil, views: bindings))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options:[], metrics:nil, views: bindings))     
    }
  // MARK: User interface
  override init(frame: CGRect) {
      super.init(frame: frame)
      commonInit()
      awakeFromNib()
  }
  required init?(coder: NSCoder) {
      super.init(coder: coder)
      commonInit()
      awakeFromNib()
  }
}

is there any thouts on that ? thank you in advance

1

There are 1 best solutions below

0
Yagnesh Dobariya On

Remove commongInit() from init method

// Add following method in CastHeader class

override func setSelected(selected: Bool, animated: Bool) {
     super.setSelected(selected, animated: animated)
     commonInit()
}

Try this solution. This may help you.