title: How do you subclass UIView and add another UIView inside of it with the same frame
body: <p>Hi I am trying to create a custom loading bar view by subclassing <code>UIView</code>. I want to create one <code>UIView</code> with a fixed frame, and another <code>UIView</code> that is inside of it. When I initialize the inner <code>UIView</code>, with the frame passed in by this method <code>override init(frame: CGRect)</code>, the two views have different origins. I want the two views to be directly on top of each other to start out. I also want to be able to update the <code>innerBar</code> by calling this <code>uploadBar.setLoadingPercentage(percent: 53.5)</code></p>
  
  <p>Here is the code:</p>
  
  <p><strong>Creating the <code>UploadBar</code></strong></p>
  
  <pre><code>let uploadBar = UploadBar(frame: CGRect(x: 40, y: 40, width: 400, height: 40))
  view.addSubview(uploadBar)
  </code></pre>
  
  <p><strong>Subclassing <code>UploadBar</code></strong></p>
  
  <pre><code>import UIKit
  
  class UploadBar: UIView {
  
      var innerBar: UIView!
  
      override init(frame: CGRect) {
          super.init(frame: frame)
  
          backgroundColor = UIColor.white
  
          innerBar = UIView(frame: frame)
          innerBar.backgroundColor = UI.customBlue()
          addSubview(innerBar)
  
      }
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  
      func setLoadingPercentage(percent: Double) {
          // change innerBar's frame and redraw
      }
  
  
  
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/C9b7q.png" rel="nofollow"><img src="https://i.stack.imgur.com/C9b7q.png" alt="The White bar is the view that I am subclassing the blue bar is &lt;code&gt;innerBar&lt;/code&gt;"></a></p>