Reputation: 1
I have this tableView in my code which I've set to be have the same leading and trailing as the view which is in it. I can confirm through coloring the background that is working.
Next I created a custom UITableViewCell to populate the tableView, the main element in it is a horizontal stackView which has other elements in it. Everything is working fine except the stackView's width which isn't occupying the entire width of the tableView.
I'm using ViewCode.
The tableView is declared like this in the mainViewController (I know it should be in a View class but it's just a small test project):
private lazy var dailyForecastTableView: UITableView = {
let table = UITableView()
table.dataSource = self
table.backgroundColor = .clear
table.register(DailyForecastTableViewCell.self, forCellReuseIdentifier: DailyForecastTableViewCell.identifier)
table.separatorColor = .white
table.translatesAutoresizingMaskIntoConstraints = false
return table
}()
And it's constraints are:
dailyForecastTableView.topAnchor.constraint(equalTo: dailyForecastLabel.bottomAnchor, constant: 16),
dailyForecastTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
dailyForecastTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
dailyForecastTableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
As I said, I've colored the background and it is indeed the full width of the screen, this seems to be all fine. The problem seems to be the stack in the tableViewCell:
private lazy var mainCellStackView: UIStackView = {
let stack = UIStackView(arrangedSubviews: [weekDayLabel, dailyIconImageView, minTemperatureLabel, maxTemperatureLabel])
stack.axis = .horizontal
stack.distribution = .fill
stack.translatesAutoresizingMaskIntoConstraints = false
stack.isLayoutMarginsRelativeArrangement = true
stack.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16)
stack.spacing = 15
return stack
}()
The constraints:
private func setupConstraints() {
mainCellStackView.setConstraintsToParent(contentView)
NSLayoutConstraint.activate([
mainCellStackView.widthAnchor.constraint(equalTo: contentView.widthAnchor),
mainCellStackView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor)
])
}
extension UIView {
func setConstraintsToParent(_ parent: UIView) {
NSLayoutConstraint.activate([
self.topAnchor.constraint(equalTo: parent.topAnchor),
self.leadingAnchor.constraint(equalTo: parent.leadingAnchor),
self.trailingAnchor.constraint(equalTo: parent.trailingAnchor),
self.bottomAnchor.constraint(equalTo: parent.bottomAnchor)
])
}
}
If I force the elements inside the stackView to have a larger width the stack occupies more space but the size wouldn't be correct in different phone sizes.
I want the stackView to occupy the entire width of the screen.
Upvotes: 0
Views: 43