title: addSubView - adding a button inside a UIView adds the same button for it's SuperView as well
body: <p>I am following this tutorial to create a rating button - <a href="https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson5.html" rel="nofollow noreferrer">https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson5.html</a></p>
  
  <p>I created a UIView inside a UIViewController inside the Storyboard by drag and drop and that UIView is controlled by the below class.</p>
  
  <pre><code>class RatingControl: UIView {
  
      required init(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
          button.backgroundColor = UIColor.redColor()
          button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown)
          addSubview(button)
      }
  
      override func intrinsicContentSize() -&gt; CGSize {
          return CGSize(width: 320, height: 44)
      }
  
      func ratingButtonTapped(button: UIButton) {
          print("Button pressed ")
      }
  }
  </code></pre>
  
  <p>The problem is when I add the UIButton, it's showing two buttons, one in it's parent UIViewController at location (x:0, y:0) in addition to the one inside the UIView. I want the button to be showing only inside the UIView inside the UIViewController. </p>
  
  <p>I researched ways to prevent it, one way is to add these buttons manually in the storyboard instead of creating them in the init, but then I am curious how it works fine in the tutorial. I am using iOS 8.3.</p>
  
  <p>Screenshot:</p>
  
  <p><a href="https://i.stack.imgur.com/ohEXp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ohEXp.png" alt="enter image description here"></a></p>