title: ios: questions regarding init(frame:) and init?(coder:)
body: <p>Apple's tutorial describes the difference between <code>init(frame:)</code> and <code>init?(coder:)</code> as </p>
  
  <blockquote>
    <p>You typically create a view in one of two ways: by programatically
    initializing the view, or by allowing the view to be loaded by the
    storyboard. There’s a corresponding initializer for each approach:
    <code>init(frame:)</code> for programatically initializing the view and
    <code>init?(coder:)</code> for loading the view from the storyboard. You will need
    to implement both of these methods in your custom control. While
    designing the app, Interface Builder programatically instantiates the
    view when you add it to the canvas. At runtime, your app loads the
    view from the storyboard.</p>
  </blockquote>
  
  <p>I feel so confused by the description "programtically initializing" and "loaded by the storyboard". Say I have a subclass of <code>UIView</code> called <code>MyView</code>, does "programtically initialization" mean I write code to add an instance of <code>MyView</code> to somewhere like:</p>
  
  <pre><code>override func viewDidLoad() {      
          super.viewDidLoad()
          let myView = MyView()  // init(frame:) get invoked here??
  }
  </code></pre>
  
  <p>while <code>init?(coder:)</code> get called when in <strong><code>Main.storyboard</code></strong> I drag a <code>UIView</code> from object library and then in the identity inspector I set its class to <code>MyView</code>?</p>
  
  <p>Besides, in my xcode project, these two methods end up with different layout for simulator and <strong><code>Main.storyboard</code></strong> with the same code:
  <a href="https://i.stack.imgur.com/ZcFJ9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZcFJ9.png" alt="enter image description here"></a></p>
  
  <pre><code>import UIKit
  
  @IBDesignable
  class RecordView: UIView {
  
      @IBInspectable
      var borderColor: UIColor = UIColor.clear {
          didSet {
              self.layer.borderColor = borderColor.cgColor
          }
      }
  
      @IBInspectable
      var borderWidth: CGFloat = 20 {
          didSet {
              layer.borderWidth = borderWidth
          }
      }
  
      @IBInspectable
      var cornerRadius: CGFloat = 100 {
          didSet {
              layer.cornerRadius = cornerRadius
          }
      }
  
      private var fillView = UIView()
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          setupFillView()
      }
  
      override init(frame: CGRect) {
          super.init(frame: frame)
          setupFillView()
      }
  
  
  
      private func setupFillView() {
          let radius = (self.cornerRadius - self.borderWidth) * 0.95
          fillView.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: radius * 2, height: radius * 2))
          fillView.center = CGPoint(x: self.bounds.midX, y: self.bounds.midY)
          fillView.layer.cornerRadius = radius
          fillView.backgroundColor = UIColor.red
          self.addSubview(fillView)
      }
  
      override func layoutSubviews() {
          super.layoutSubviews()
      }
  
      func didClick() {
          UIView.animate(withDuration: 1.0, animations: {
              self.fillView.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
          }) { (true) in
              print()
          }
      }
  }
  </code></pre>
  
  <p>Why do they behave differently? (I drag a <code>UIView</code> from object library and set its class to RecordView)</p>