title: Performance issues when creating UIView Sublclass from xib (Swift 3)
body: <h3>The idea</h3>
  
  <hr>
  
  <p>In my application, I want to create a <strong>custom button</strong> by subclassing a UIView, including an icon <strong>image</strong> and a <strong>label</strong>. When the user tap the button, the icon and the label color change and I use the information that the button is pressed in an other class. (Not really important).</p>
  
  <h3>The problem</h3>
  
  <hr>
  
  <ul>
  <li>I need to display 10 to 20 buttons of that kind in my
  UIViewController. The first button is working fine, but <strong>button
  after button, they become less reactive</strong>. The last one need up to 1
  seconde to change it’s color.</li>
  <li>Also, Xcode doesn't really like that; my Storyboard where I
  implemented my custom UIView become <strong>extremely slow</strong> and the agent
  rendering the views often crash.</li>
  <li>Moreover, my app takes <strong>forever to load.</strong></li>
  </ul>
  
  <h3>The way I've done that</h3>
  
  <hr>
  
  <p>I have created a class "<code>@IBDesignable class IconButton: UIView{}</code>" and I load the view form a xib file. </p>
  
  <p>There is two outlets : </p>
  
  <ol>
  <li><code>@IBOutlet var iconImageOutlet: UIImageView!</code></li>
  <li><code>@IBOutlet var titleLabelOutlet: UILabel!</code></li>
  </ol>
  
  <p>I want my button’s title and image to be IBInspectable. So I do this :</p>
  
  <pre><code>@IBInspectable var buttonText:String = "text_01"{
      didSet{
          titleLabelOutlet.text = buttonText
      }
  }
  
  @IBInspectable var buttonImage:UIImage? = UIImage(named: "image_01"){
      didSet{
          iconImageOutlet.image = buttonImagesetColor()
      }
  }
  </code></pre>
  
  <p>I have implemented these methods to instantiate the view : </p>
  
  <pre><code>override init(frame: CGRect) {
      super.init(frame: frame)
      xibSetup()
  }
  
  required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
      xibSetup()
  }
  
  override func prepareForInterfaceBuilder(){
      super.prepareForInterfaceBuilder()
      xibSetup()
  }
  
  func xibSetup() {
      view = loadViewFromNib()
      view.frame = boundsview.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
      addSubview(view)
      setColor()
  }
  
  func loadViewFromNib() -&gt; UIView {
      let bundle = Bundle(for: type(of: self))
      let nib = UINib(nibName: "IconButton", bundle: bundle)
      let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
  
      return view
  }
  
  private func setColor(){
      iconImageOutlet.image = iconImageOutlet.image!.withRenderingMode(.alwaysTemplate)
      UIView.animate(withDuration: 0.1) {
          if self.isActive {
              self.iconImageOutlet.tintColor = UIColor.pindexBlue()
              self.titleLabelOutlet.textColor = UIColor.pindexBlue()
  
              let changeColor = CATransition()
              changeColor.type = kCATransitionFade
              changeColor.duration = 0.1
  
              CATransaction.begin()
  
              CATransaction.setCompletionBlock {
                      self.titleLabelOutlet.textColor = UIColor.blue
                      self.titleLabelOutlet.layer.add(changeColor, forKey: nil)
              }
              self.titleLabelOutlet.textColor = UIColor.blue
              self.titleLabelOutlet.layer.add(changeColor, forKey: nil)
  
              CATransaction.commit()
  
          } else {
              self.iconImageOutlet.tintColor = UIColor.blue
              self.titleLabelOutlet.textColor = UIColor.gray
  
              let changeColor = CATransition()
              changeColor.type = kCATransitionFade
              changeColor.duration = 0.1
  
              CATransaction.begin()
  
              CATransaction.setCompletionBlock {
                  self.titleLabelOutlet.textColor = UIColor.gray
                  self.titleLabelOutlet.layer.add(changeColor, forKey: nil)
                  }
              self.titleLabelOutlet.textColor = UIColor.gray
              self.titleLabelOutlet.layer.add(changeColor, forKey: nil)
  
              CATransaction.commit()
          }
      }  
  }
  
  private var isActive:Bool = false
  @IBAction func buttonPressed(_ sender: Any) {
      isActive = !isActive
      setColor()
  }
  </code></pre>
  
  <h3>Conclusion</h3>
  
  <hr>
  
  <p>I really don't know what I'm doing wrong, and nobody seams to have to same problem.</p>
  
  <p>So if anyone have suppositions, thank you :) </p>
  
  <p><em>PS: Sorry for my bad English</em></p>