title: Table Cells get attached to top
body: <p>If i test the app on large screen simulator everything works well, but the moment i test the code in small screen simulator, there is no error but the cells that are not visible and can be seen only while scrolling gets attached to top of screen, strangely enough if i scroll down again i can see the labels and text fields looking like they are moving from top to their original locations, but they do not and remain on screen top, i am posting the image , where could i be making the mistake below is code of file and image , the misbehaviour happens when i scroll</p>
  <pre><code>import UIKit
  import CoreData
  
  class RestaurantAddController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UITextViewDelegate, UIImagePickerControllerDelegate &amp; UINavigationControllerDelegate {
      
      
      
      let displayInputForm = UITableView()
      var restaurant: Restaurant!
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return 6
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = displayInputForm.dequeueReusableCell(withIdentifier: String(describing: RestaurantAddViewCells.self), for: indexPath) as! RestaurantAddViewCells
          cell.heightAnchor.constraint(greaterThanOrEqualToConstant: 100).isActive = true
          view.addSubview(cell)
          //   view.addSubview(cell.contentView)
          view.addSubview(cell.imageToUpload)
          view.addSubview(cell.nameToUploadLabel)
          view.addSubview(cell.nameToUploadTextField)
          view.addSubview(cell.typeToUploadLabel)
          view.addSubview(cell.typeToUploadTextField)
          view.addSubview(cell.locationToUploadLabel)
          view.addSubview(cell.locationToUploadTextField)
          view.addSubview(cell.phoneToUploadLabel)
          view.addSubview(cell.phoneToUPloadTextField)
          view.addSubview(cell.summartyToUploadLabel)
          view.addSubview(cell.summaryToUploadTextView)
          //Add to cell via stackView
          let stackEntry = UIStackView()
          view.addSubview(stackEntry)
          stackEntry.translatesAutoresizingMaskIntoConstraints = false
          stackEntry.alignment = .top
          stackEntry.axis = .vertical
          stackEntry.spacing = 5.0
          stackEntry.distribution = .fill
          
          stackEntry.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: 10).isActive = true
          stackEntry.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: -10).isActive = true
          stackEntry.topAnchor.constraint(equalTo: cell.topAnchor, constant: 20).isActive = true
          //add scroll view
          //        let scrollView = UIScrollView()
          //        view.addSubview(scrollView)
          //        scrollView.translatesAutoresizingMaskIntoConstraints = false
          //        scrollView.topAnchor.constraint(equalTo: displayInputForm.topAnchor).isActive = true
          //        scrollView.bottomAnchor.constraint(equalTo: displayInputForm.bottomAnchor).isActive = true
          //        scrollView.leadingAnchor.constraint(equalTo: displayInputForm.leadingAnchor).isActive = true
          //        scrollView.trailingAnchor.constraint(equalTo: displayInputForm.trailingAnchor).isActive = true
          //        scrollView.addSubview(stackEntry)
          //MARK:- give custom font
          let font = UIFont(name: &quot;Rubik-Medium&quot;, size: 18)
          let fontMetrics = UIFontMetrics(forTextStyle: .body)
          
          let labels = [cell.locationToUploadLabel, cell.typeToUploadLabel, cell.nameToUploadLabel, cell.phoneToUploadLabel, cell.summartyToUploadLabel]
          labels.forEach { label in
              label.font = fontMetrics.scaledFont(for: font!)
          }
          
          
          let textCusotm = [cell.nameToUploadTextField, cell.typeToUploadTextField, cell.locationToUploadTextField, cell.phoneToUPloadTextField]
          textCusotm.forEach { (fields) in
              fields.font = fontMetrics.scaledFont(for: font!)
              
          }
          cell.summaryToUploadTextView.font = fontMetrics.scaledFont(for: font!)
          
          //MARK:- add cell per row
          switch indexPath.row {
              
          case 0:
              
              cell.heightAnchor.constraint(greaterThanOrEqualToConstant: 250).isActive = true
              let buttonImage = UIImageView()
              cell.backgroundColor = UIColor.lightGray
              buttonImage.translatesAutoresizingMaskIntoConstraints = false
              buttonImage.image = UIImage(named: &quot;photo&quot;)
              view.addSubview(buttonImage)
              buttonImage.heightAnchor.constraint(equalToConstant: 30).isActive = true
              buttonImage.widthAnchor.constraint(equalToConstant: 30).isActive = true
              
              buttonImage.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
              
              buttonImage.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
              
              return cell
          case 1:
              cell.selectionStyle = .none
              cell.nameToUploadLabel.text = &quot;Name of Hotel:&quot;
              cell.nameToUploadTextField.placeholder = &quot;Enter Hotel Name&quot;
              cell.nameToUploadTextField.tag = 1
              cell.nameToUploadTextField.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: -10).isActive = true
              stackEntry.addArrangedSubview(cell.nameToUploadLabel)
              stackEntry.addArrangedSubview(cell.nameToUploadTextField)
              //   cell.nameToUploadTextField.becomeFirstResponder()
              cell.nameToUploadTextField.delegate = self
              return cell
              
              
          case 2:
              cell.selectionStyle = .none
              
              cell.typeToUploadLabel.text = &quot;Type Of Hotel:&quot;
              cell.typeToUploadTextField.placeholder = &quot;Name type of hotel&quot;
              cell.typeToUploadTextField.tag = 2
              cell.typeToUploadTextField.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: -10).isActive = true
              stackEntry.addArrangedSubview(cell.typeToUploadLabel)
              stackEntry.addArrangedSubview(cell.typeToUploadTextField)
              cell.typeToUploadTextField.delegate = self
              return cell
              
          case 3:
              cell.selectionStyle = .none
              cell.locationToUploadLabel.text = &quot;Location of Hotel:&quot;
              cell.locationToUploadTextField.placeholder = &quot;Enter Hotel Location&quot;
              cell.locationToUploadTextField.tag = 3
              cell.locationToUploadTextField.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: -10).isActive = true
              stackEntry.addArrangedSubview(cell.locationToUploadLabel)
              stackEntry.addArrangedSubview(cell.locationToUploadTextField)
              cell.locationToUploadTextField.delegate = self
              return cell
              
          case 4:
              cell.selectionStyle = .none
              cell.phoneToUploadLabel.text = &quot;Phone:&quot;
              cell.phoneToUPloadTextField.placeholder = &quot;Enter Phone&quot;
              cell.phoneToUPloadTextField.tag = 4
              cell.phoneToUPloadTextField.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: -10).isActive = true
              stackEntry.addArrangedSubview(cell.phoneToUploadLabel)
              stackEntry.addArrangedSubview(cell.phoneToUPloadTextField)
              cell.phoneToUPloadTextField.delegate = self
              return cell
              
          case 5:
              cell.selectionStyle = .none
              cell.summartyToUploadLabel.text = &quot;Enter Summary:&quot;
              //cell.summaryToUploadTextView.place
              cell.summaryToUploadTextView.tag = 5
              cell.summaryToUploadTextView.heightAnchor.constraint(equalToConstant: 200).isActive = true
              cell.summaryToUploadTextView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 20).isActive = true
              cell.summaryToUploadTextView.layer.borderWidth = 1
              cell.summaryToUploadTextView.layer.cornerRadius = 5.0
              cell.summaryToUploadTextView.layer.borderColor = UIColor.lightGray.cgColor
              stackEntry.addArrangedSubview(cell.summartyToUploadLabel)
              stackEntry.addArrangedSubview(cell.summaryToUploadTextView)
              cell.summaryToUploadTextView.delegate = self
              return cell
              
          default:
              fatalError(&quot;No data found&quot;)
              
          }
      }
      
      
      override func viewDidLoad() {
          super.viewDidLoad()
          displayInputForm.delegate = self
          displayInputForm.dataSource = self
          //MARK:- Add scroll view
          
          view.addSubview(displayInputForm)
          
          
          
          
          let footerView = UIView()
          displayInputForm.tableFooterView = footerView
          // Do any additional setup after loading the view.
          
          //Add pinning of table
          displayInputForm.translatesAutoresizingMaskIntoConstraints = false
          self.displayInputForm.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
          self.displayInputForm.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
          self.displayInputForm.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
          self.displayInputForm.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
          
          //Change the back button to arrow
          let imgBack = UIImage(named: &quot;back&quot;)
          
          navigationController?.navigationBar.backIndicatorImage = imgBack
          navigationController?.navigationBar.backIndicatorTransitionMaskImage = imgBack
          
          navigationItem.leftItemsSupplementBackButton = true
          navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: &quot;&quot;, style: .plain, target: self, action: nil)
          
          // Register a cell
          displayInputForm.register(RestaurantAddViewCells.self, forCellReuseIdentifier: String(describing: RestaurantAddViewCells.self))
          
          //MARK:- add the cancel and save button
          self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: &quot;close&quot;), style: .done, target: self, action: #selector(closeRestaurant))
          self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: &quot;save&quot;), style: .plain, target: self, action: #selector(saveRestaurantData))
          
      }
      
      
      //MARK:- close function
      @objc func closeRestaurant() {
          self.navigationController?.popViewController(animated: true)
      }
      
      //MARK:- save function
      @objc func saveRestaurantData() {
          
          let indexPath0 = IndexPath(row: 1, section: 0)
          let cell0 = displayInputForm.cellForRow(at: indexPath0) as! RestaurantAddViewCells
          let nameVal = cell0.nameToUploadTextField.text
          
          let indexPath1 = IndexPath(row: 2, section: 0)
          let cell1 = displayInputForm.cellForRow(at: indexPath1) as! RestaurantAddViewCells
          let typeVal = cell1.typeToUploadTextField.text
          
          let indexPath2 = IndexPath(row: 3, section: 0)
          let cell2 = displayInputForm.cellForRow(at: indexPath2) as! RestaurantAddViewCells
          let locationVal = cell2.locationToUploadTextField.text
          
          let indexPath3 = IndexPath(row: 4, section: 0)
          let cell3 = displayInputForm.cellForRow(at: indexPath3) as! RestaurantAddViewCells
          let phoneVal = cell3.phoneToUPloadTextField.text
          
          let indexPath4 = IndexPath(row: 5, section: 0)
          let cell4 = displayInputForm.cellForRow(at: indexPath4) as! RestaurantAddViewCells
          let summaryVal = cell4.summaryToUploadTextView.text
          
          
          let indexPath5 = IndexPath(row: 0, section: 0)
          let cell5 = displayInputForm.cellForRow(at: indexPath5) as! RestaurantAddViewCells
          let imageVal = cell5.imageToUpload.image
          
          
          if (imageVal == nil || nameVal == &quot;&quot; || typeVal == &quot;&quot; || locationVal == &quot;&quot; || phoneVal == &quot;&quot; || summaryVal == &quot;&quot;) {
              let saveAlertController = UIAlertController(title: &quot;Empty Fields&quot;, message: &quot;Fill all the fileds&quot;, preferredStyle: .alert)
              
              let saveAlertAction = UIAlertAction(title: &quot;OK&quot;, style: .cancel, handler: nil)
              
              saveAlertController.addAction(saveAlertAction)
              
              self.present(saveAlertController, animated: true) {
                  let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dissMissAlertController))
                  saveAlertController.view.superview?.subviews[0].addGestureRecognizer(tapGesture)
              }
          }
              
          else {
              if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
                  let context = appDelegate.persistentContainer.viewContext
                  restaurant = Restaurant(context: context)
                  restaurant.name = nameVal
                  restaurant.type = typeVal
                  restaurant.location = locationVal
                  restaurant.phone = phoneVal
                  restaurant.summary = summaryVal
                  
                  if let restaurantImage = cell5.imageToUpload.image {
                      restaurant.image = restaurantImage.pngData()
                  }
                  
                  print(&quot;data saved&quot;)
                  appDelegate.saveContext()
              }
          }
          
          self.navigationController?.popViewController(animated: false)
          
          
      }
      
      
      
      /*
       // MARK: - Navigation
       
       // In a storyboard-based application, you will often want to do a little preparation before navigation
       override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       // Get the new view controller using segue.destination.
       // Pass the selected object to the new view controller.
       }
       */
      
      override func viewWillAppear(_ animated: Bool) {
          
          self.title = &quot;Add Restaurants&quot;
          
      }
      
      
      //MARK:- check if textfield can be moved to next one on return key press
      
      func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
          if let nextField = view.viewWithTag(textField.tag + 1) as? UITextField {
              textField.resignFirstResponder()
              nextField.becomeFirstResponder()
              //  nextField.endEditing(false)
              
          }
          
          return true
      }
      //MARK:- add sction to row clicks
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          if indexPath.row == 0 {
              let UIImageAlertController = UIAlertController(title: &quot;&quot;, message: &quot;Select an option&quot;, preferredStyle: .actionSheet)
              
              let photoGalleryAction = UIAlertAction(title: &quot;PhotoLibrary&quot;, style: .default) { (action) in
                  if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                      let imageController = UIImagePickerController()
                      imageController.allowsEditing = false
                      imageController.sourceType = .photoLibrary
                      imageController.delegate = self
                      self.present(imageController, animated: true, completion: nil)
                  }
              }
              
              UIImageAlertController.addAction(photoGalleryAction)
              
              
              let cameraPicturesAction = UIAlertAction(title: &quot;Camera&quot;, style: .default) { (action) in
                  if UIImagePickerController.isSourceTypeAvailable(.camera) {
                      let cameraController = UIImagePickerController()
                      cameraController.allowsEditing = false
                      cameraController.sourceType = .camera
                      cameraController.delegate = self
                      
                      self.present(cameraController, animated: true, completion: nil)
                  }
              }
              
              UIImageAlertController.addAction(cameraPicturesAction)
              self.present(UIImageAlertController, animated: true) {
                  let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dissMissAlertController))
                  UIImageAlertController.view.superview?.subviews[0].addGestureRecognizer(tapGesture)
              }
          }
          
          
      }
      
      @objc func dissMissAlertController() {
          self.dismiss(animated: true, completion: nil)
      }
      
      
      //MARK:- function to add and select a picture and then add it
      
      func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
          if let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
              if let indexPath = displayInputForm.indexPathForSelectedRow {
                  let cell = displayInputForm.cellForRow(at: indexPath) as! RestaurantAddViewCells
                  cell.imageToUpload.translatesAutoresizingMaskIntoConstraints = false
                  cell.imageToUpload.widthAnchor.constraint(equalTo: cell.widthAnchor).isActive = true
                  cell.imageToUpload.heightAnchor.constraint(equalToConstant: 250).isActive = true
                  cell.imageToUpload.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
                  cell.imageToUpload.image = selectedImage
                  cell.contentMode = .scaleAspectFill
                  cell.clipsToBounds = true
                  
              }
              
              dismiss(animated: true, completion: nil)
          }
      }
      
      
      
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/whrDh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/whrDh.png" alt="enter image description here" /></a></p>
  <p><strong>Solution</strong></p>
  <p>add this line in viewdidload, where the tableName is the instance of your table</p>
  <pre><code>&lt;tableName&gt;.contentInsetAdjustmentBehavior = .never
  </code></pre>