title: UICollectionView - When the keyboard appears the entire collection view is shifted with the keyboard
body: <p>This isn't my desired effect. This only happens when the collection view is set to horizontal flow layout. I've seen a few other posts regarding this very same issue but none of the provided answers have worked. Has anyone found a solution?</p>
  
  <p>I've provided two screenshots showing a UITextField before the keyboard is triggered and after. As you can see the UITextField along with the entire collection view (which can't be seen) is pushed up along with the keyboard. Usually the keyboard is overlayed having no effect on the views.</p>
  
  <p>Before</p>
  
  <p><a href="https://i.stack.imgur.com/ZplQC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZplQC.png" alt="Before"></a></p>
  
  <p>After</p>
  
  <p><a href="https://i.stack.imgur.com/DaOZA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DaOZA.png" alt="After"></a></p>
  
  <p><strong>Update</strong>
  Code provided. The method I used to implement doesn't involve a Storyboard.</p>
  
  <pre><code>class AppDelegate: UIResponder, UIApplicationDelegate {
  
  var window: UIWindow?
  
  
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {
      // Override point for customization after application launch.
      window = UIWindow()
      window?.makeKeyAndVisible()
  
      let layout = UICollectionViewFlowLayout()
      layout.scrollDirection = .horizontal
      let rvc = ViewController(collectionViewLayout: layout)
      window?.rootViewController = rvc
  
      return true
  }
  
  // .... other boilerplate code provided by Apple when you make a new App
  }
  </code></pre>
  
  <p>ViewController.swift</p>
  
  <pre><code>class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UITextFieldDelegate {
      let homeCellId = "homeCellId"
      let worldCellId = "worldCellId"
  
      override func viewDidLoad() {
          super.viewDidLoad()
          self.view.backgroundColor = UIColor.white
          collectionView?.register(HomeCell.self, forCellWithReuseIdentifier: homeCellId)
          collectionView?.register(WorldCell.self, forCellWithReuseIdentifier: worldCellId)
  collectionView?.isPagingEnabled = true
          // Do any additional setup after loading the view, typically from a nib.
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 2
      }
  
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
          return CGSize(width: view.frame.width, height: view.frame.height)
      }
  
      override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          print(indexPath.item)
          let identifier: String
          if indexPath.item == 0 {
              identifier = homeCellId
          } else if indexPath.item == 1 {
              identifier = worldCellId
          }
          else {
              identifier = homeCellId
          }
  
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
          cell.backgroundColor = UIColor.blue
          return cell
      }
  
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -&gt; CGFloat {
          return 0
      }
  }
  </code></pre>
  
  <p>MyCell.swift</p>
  
  <pre><code>class MyCell: UICollectionViewCell {
  
      override init(frame: CGRect) {
          super.init(frame: frame)
          self.setupView()
      }
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  
      func setupView() {
  
      }
  
  
  }
  </code></pre>
  
  <p>HomeCell.swift</p>
  
  <pre><code>class HomeCell: MyCell {
      let weightField: UITextField = UITextField(frame: .zero)
      override func setupView() {
          super.setupView()
          print("HomeCell")
          weightField.backgroundColor = UIColor.white
          weightField.translatesAutoresizingMaskIntoConstraints = false
          weightField.keyboardType = UIKeyboardType.default
          self.addSubview(weightField)
  
          weightField.topAnchor.constraint(equalTo: self.topAnchor, constant: 200).isActive = true
          weightField.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 50).isActive = true
      }
  
  
  }
  </code></pre>
  
  <p>WorldCell.swift</p>
  
  <p>// same as HomeCell</p>
  
  <pre><code>class WorldCell: MyCell {
  
      override func setupView() {
          super.setupView()
          print("worldcell")
      }
  
  
  }
  </code></pre>