Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
eberhapa
51,495 PointsProblem with width and height of tableviewheader contenview
I have a problem with my TableViewHeaderCells contentView size. I'm dequeueing a TableViewCell in viewForHeaderInSection, adding two labels and constraints. I'm also setting the height for the header to a bigger height than the default value. The contentView doesn't automatically resizes to the same height and also not to the same width.
Exactly the same approach is working for my tableViewCells without any issue. So why isn't it behaving for the header like for the normal cell?
The black frame on the picture below is the headerCell and the green frame it's contentView. The yellow frames are UIViews in a customCell contentView. Like I said, here it's expecting like it should. The code for the header is below the picture.
class ContractHeader: UITableViewCell {
static let reuseIdentifier = "\(ContractHeader.self)"
lazy var headerLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var priceLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textAlignment = .right
return label
}()
override func layoutSubviews() {
contentView.addSubview(headerLabel)
contentView.addSubview(priceLabel)
NSLayoutConstraint.activate([
headerLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 15),
headerLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -15),
headerLabel.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 1/2),
priceLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -15),
priceLabel.bottomAnchor.constraint(equalTo: headerLabel.bottomAnchor),
priceLabel.leftAnchor.constraint(equalTo: headerLabel.rightAnchor)
])
}
}
