0

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

mzoko .T.K
  • 33
  • 5

1 Answers1

0

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.

Yagnesh Dobariya
  • 2,241
  • 19
  • 29