title: UICollectionView - Not Redrawing Properly When App is Reopened
body: <p>For some reason my collection view code works as expected when I Launch the emulator but when I hit the home button to close the app and then reopen it the 3 columns collapse on the right to one column.  The only thing I have in Storyboard is a view. Everything is done via code.  Xcode 11.1 / Swift 5.</p>
  
  <p>Here is what it looks like on the first open:</p>
  
  <p><a href="https://i.stack.imgur.com/dlAQ1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dlAQ1.png" alt="enter image description here"></a></p>
  
  <p>And on the reopen:</p>
  
  <p><a href="https://i.stack.imgur.com/26iUh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/26iUh.png" alt="enter image description here"></a></p>
  
  <p>Here is all of the code.</p>
  
  <pre><code>
  class Online: UIViewController {
  
      weak var collectionView: UICollectionView!
      var onlineArray = [[String:AnyObject]]()
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
          collectionView.translatesAutoresizingMaskIntoConstraints = false
  
          self.view.addSubview(collectionView)
  
          NSLayoutConstraint.activate([
              self.view.topAnchor.constraint(equalTo: collectionView.topAnchor),
              self.view.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor),
              self.view.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor),
              self.view.trailingAnchor.constraint(equalTo: collectionView.trailingAnchor),
          ])
  
          self.collectionView = collectionView
  
          self.collectionView.dataSource = self
          self.collectionView.delegate = self
          self.collectionView.register(Cell.self, forCellWithReuseIdentifier: Cell.identifier)
          self.collectionView.alwaysBounceVertical = true
      }
  
  }
  
  extension Online: UICollectionViewDataSource {
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 10
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier, for: indexPath) as! Cell
  
          cell.textLabel1.text = "A"
          cell.textLabel2.text = "B"
          cell.textLabel3.text = "C"
  
          cell.textLabel1.backgroundColor = .orange
          cell.textLabel2.backgroundColor = .blue
          cell.textLabel3.backgroundColor = .green
  
          return cell
      }
  
  }
  
  extension Online: UICollectionViewDelegateFlowLayout {
  
      func collectionView(_ collectionView: UICollectionView,
                          layout collectionViewLayout: UICollectionViewLayout,
                          sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
          return CGSize(width: collectionView.bounds.width, height: 30)
      }
  
      func collectionView(_ collectionView: UICollectionView,
                          layout collectionViewLayout: UICollectionViewLayout,
                          insetForSectionAt section: Int) -&gt; UIEdgeInsets {
          return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //.zero
      }
  
      func collectionView(_ collectionView: UICollectionView,
                          layout collectionViewLayout: UICollectionViewLayout,
                          minimumInteritemSpacingForSectionAt section: Int) -&gt; CGFloat {
          return 0
      }
  
      func collectionView(_ collectionView: UICollectionView,
                          layout collectionViewLayout: UICollectionViewLayout,
                          minimumLineSpacingForSectionAt section: Int) -&gt; CGFloat {
          return 0
      }
  
  }
  
  class Cell: UICollectionViewCell {
  
      static var identifier: String = "Cell"
  
      weak var textLabel1: UILabel!
      weak var textLabel2: UILabel!
      weak var textLabel3: UILabel!
  
      override init(frame: CGRect) {
          super.init(frame: frame)
  
          let width = self.contentView.frame.size.width
  
          let textLabel1 = UILabel(frame: CGRect(x: 10, y: 0, width: 50, height: 20))
          let textLabel2 = UILabel(frame: CGRect(x: width/3, y: 0, width: 50, height: 20))
          let textLabel3 = UILabel(frame: CGRect(x: (width/3)*2, y: 0, width: 50, height: 20))
  
          textLabel1.translatesAutoresizingMaskIntoConstraints = false
          textLabel2.translatesAutoresizingMaskIntoConstraints = false
          textLabel3.translatesAutoresizingMaskIntoConstraints = false
  
          self.contentView.addSubview(textLabel1)
          self.contentView.addSubview(textLabel2)
          self.contentView.addSubview(textLabel3)
  
          self.textLabel1 = textLabel1
          self.textLabel2 = textLabel2
          self.textLabel3 = textLabel3
          self.reset()
      }
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  
      override func prepareForReuse() {
          super.prepareForReuse()
          self.reset()
      }
  
      func reset() {
          self.textLabel1.textAlignment = .left
          self.textLabel2.textAlignment = .center
          self.textLabel3.textAlignment = .right
      }
  
  }
  
  </code></pre>