The Problem
In your question title, you asked how to solve an issue where "Swift TableViewCell xib doesn't have constraints." Additionally in the bounty, you specify that the answer should be:
An adequate answer showing why constraints are lost when moved to a
xib, when they exist in the xib, and how to address this problem.
From my understanding, your question has two parts:
- Issue where a custom TableViewCell xib does not have constraints
- Why constraints are lost when copy/pasted from Storyboard prototype to a xib
For issue #1, I used almost all of the same constraints as you had displayed in your screenshot in a custom cell xib and it worked. I will explain in more detail below.
For issue #2, I found that constraints are not lost. You may need to share your project so that others can replicate the issue you are having. I copy/pasted a Storyboard prototype cell with similar constraints over to a xib and I did not have any issues with constraints. So I can confirm that the copy/paste feature does work.
Solution Demo
I created the following demo to test your issue. Refer to the screenshot below. The tableView contains six cells. The 1st and 2nd are the same and have reuse identifier MyCell
. The 3rd and 4th are the same and have reuse identifier MyCopyPasteCell
. The 5th and 6th are the same and have reuse identifier MyPrototypeCell
.
MyCell
exists in a xib and uses nearly all the same constraints that you showed in your screenshot. As you can see, there are no constraints issues. MyCopyPasteCell
uses similar constraints and was copy/pasted from a Storyboard prototype over to a xib. MyPrototypeCell
is the original prototype that was copied. These last four cells look exactly the same even though the prototype was copied over to the xib. The constraints carried over from prototype to xib without an issue.
Constraints
In the code below, I list all the constraints that you showed in your screenshot for your ContentView. (Note that I also implemented the height=20
, aspect=1:1
, and height=75
constraints, although they are not listed below.) Two constraints are commented out since I did not use them. I also added one more constraint to replace another unused one.
// bottomMargin = Profile Image View.bottom + 188.5
bottomMargin = Profile Image.bottom + 17
Profile Image View.top = Username Label.top
Profile Image View.leading = leadingMargin + 2
Profile Image View.top = topMargin + 20
Username Label.leading = Content Text View.leading
// Username Label.top = topMargin + 20
Username Label.trailing = Content Text View.trailing
Username Label.leading = Profile Image View.trailing + 15
trailingMargin = Username Label.trailing + 10
Content Text View.top = Username Label.bottom + 5
Content Text View.leading = leadingMargin + 92
bottomMargin = Content Text View.bottom + 20
The first commented constraint // bottomMargin = Profile Image View.bottom + 188.5
did not make sense as it would separate the bottom of the image from the bottom of the cell by 188.5. This also did not match your Prototype cell in Storyboard (Used to work) screenshot at all. I replaced it with bottomMargin = Profile Image.bottom + 17
, which just replaces the 188.5 with 17.
The second commented constraint // Username Label.top = topMargin + 20
(which separates username and topMargin by 20) can technically work. However, its functionality is redundant to Profile Image.top = topMargin + 20
(which separates Profile Image and topMargin by 20) and Profile Image View.top = Username Label.top
(which sets the Profile Image and Username to the same top separation i.e. by 20).
MyTableViewController
The following is my view controller. Basically I create three sections with two cells/rows per section. MyCell
and MyCopyPasteTableViewCell
are from a xib and are registered in the viewDidLoad()
. MyPrototypeCell
is from the Storyboard and is not registered in viewDidLoad()
.
// MyTableViewController.swift
import UIKit
class MyTableViewController: UITableViewController {
let myCell = "MyCell"
let myCopyPasteCell = "MyCopyPasteTableViewCell"
let myPrototypeCell = "MyPrototypeCell"
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: myCell, bundle: nil), forCellReuseIdentifier: myCell)
tableView.register(UINib(nibName: myCopyPasteCell, bundle: nil), forCellReuseIdentifier: myCopyPasteCell)
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: myCell, for: indexPath)
return cell
} else if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: myCopyPasteCell, for: indexPath)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: myPrototypeCell, for: indexPath)
return cell
}
}
}
Github link
https://github.com/starkindustries/TableViewCellConstraintsTest