title: CustomView with XIB - indefinite looping in init coder method
body: <p>In my project I am trying to create a custom UIView from a XIB file. I followed few tutorials and arrived at below code to load</p>
  
  <pre><code>import UIKit
  
  class StorePricing: UIView {
  
      override init(frame: CGRect) {
  
          super.init(frame: frame)
          self.setupView()
      }
  
      required init?(coder aDecoder: NSCoder) {
  
          super.init(coder: aDecoder)
          self.setupView()
      }
  
      private func setupView() {
  
          let view = self.loadViewFromXib()
  
          view.frame = self.bounds
          view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
  
          self.addSubview(view)
      }
  
      private func loadViewFromXib() -&gt; UIView {
  
          let bundle = Bundle(for: type(of: self))
          let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
          let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
  
          return view
  
      }
  
  }
  </code></pre>
  
  <p>When I add this custom view in another view my app crashed and I noticed the init call is called in a indefinite loop. The call hierarchy is as follows in my custom view</p>
  
  <ol>
  <li>Call to init coder</li>
  <li>Call to setupView()</li>
  <li>Call to loadViewFromXib()</li>
  </ol>
  
  <p>nib.instantiate calls init coder and the loop becomes indefinite</p>
  
  <p>Any suggestions on how to solve this issue?</p>