title: Generic UIView Initializer with closure in swift
body: <p>I want to write a generic UIView initializer so I can initialize UIViews by passing the configuration closure in initializer.What I want is the below syntax to work for all UIView subclasses.</p>
  
  <pre><code>let button = UIButton() {
      $0.backgroundColor = UIColor.red
      $0.frame = CGRect(x: 220, y: 30, width: 100, height: 100)
      $0.setTitle("Test", for: .normal)
  }
  </code></pre>
  
  <p>I have written the convenience initializer in UIView extension <strong>but with this I am not able to set UIView subclasses properties like the setTitle(_:for) property for UIButton</strong> because in closure it is always sending UIView type parameter instead of the specific subclass type.</p>
  
  <p>Here is my initializer.</p>
  
  <pre><code>extension UIView {
  
      convenience init&lt;T: UIView&gt;(_ configurations: (T) -&gt; Void) {
          self.init()
  
          configurations(self as! T)
      }
  }
  </code></pre>
  
  <p>Any suggestions will be appreciated.</p>
  
  <p><strong>NOTE:</strong> I was able to achieve the above behaviour of initializing UIView and subtypes with closure using protocol but I wonder if this can be achieved this way i.e. Writing convenience initializer in UIView extension without any additional protocol.</p>