title: Swift: Screen Freezing from time to time when Dismissing a ViewController displaying a picture over the current context
body: <p>I am having some troubles with: The screen freezing from time to time when Dismissing the ViewController displaying a picture over the current context.</p>
  
  <p>May someone provide me some insights on how to fix this problem?</p>
  
  <p>A sample of my codes is found below: </p>
  
  <p>import UIKit</p>
  
  <p>class ViewControllerCell: UICollectionViewCell {</p>
  
  <pre><code>override init(frame: CGRect) {
      super.init(frame: frame)
  
      backgroundColor = UIColor.white
  
      addSubview(showPhotoButton)
  
      showPhotoButton.leftAnchor.constraint(equalTo: leftAnchor, constant: 200).isActive = true
      showPhotoButton.bottomAnchor.constraint(equalTo: topAnchor, constant: 160).isActive = true
      showPhotoButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
      showPhotoButton.widthAnchor.constraint(equalToConstant: 70).isActive = true
  
  }
  
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
  
  
  lazy var showPhotoButton: UIButton = {
      let button = UIButton(type: .system)
      button.translatesAutoresizingMaskIntoConstraints = false
      button.setTitle("Show", for: .normal)
      button.addTarget(self, action: #selector(showSale), for: .touchUpInside)
      button.setTitleColor(UIColor(r: 120, g: 80, b: 255), for: .normal)
      return button
  }()
  
  
  @objc func showSale() {
      let popupViewController = PopupViewController()
      popupViewController.modalPresentationStyle = .overCurrentContext
      popupViewController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
      window!.rootViewController?.present(PopupViewController, animated: true, completion: nil)
  }
  </code></pre>
  
  <p>}</p>
  
  <p>import UIKit</p>
  
  <p>class SalePopupViewController: UIViewController {</p>
  
  <pre><code>override func viewDidLoad() {
      view.backgroundColor = UIColor(white: 1, alpha: 0.60)
      view.isOpaque = false
  
      view.addSubview(rebateImage)
      view.addSubview(dismissButton)
  
      dismissButton.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
      dismissButton.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
      dismissButton.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
      dismissButton.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
  
      rebateImage.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
      rebateImage.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -35).isActive = true
      rebateImage.heightAnchor.constraint(equalToConstant: 290).isActive = true
      rebateImage.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1).isActive = true
  
  
  }
  
  let dismissButton: UIButton = {
      let button = UIButton(type: .system)
      button.addTarget(self, action: #selector(dismissPopup), for: .touchUpInside)
      button.translatesAutoresizingMaskIntoConstraints = false
      return button
  }()
  
  let rebateImage: UIImageView = {
      let image = UIImageView()
      image.translatesAutoresizingMaskIntoConstraints = false
      image.layer.masksToBounds = false
      image.layer.cornerRadius = 2
      image.contentMode = .scaleAspectFill
      image.clipsToBounds = true
      image.image = UIImage(named: "SaleCostco")
      return image
  }()
  
  @objc func dismissPopup() {
  
      DispatchQueue.global(qos: .userInitiated).async {
          DispatchQueue.main.async {
              self.dismiss(animated: true, completion: nil)
          }
      }
  
  
  }
  </code></pre>
  
  <p>}</p>