My app displays business information in a tableView and mapView. In the tableView, I measure the size of the text that populates the business name label and adjust the cell height appropriately for longer names in a custom heightForRowAt function. It works well:
In my mapView, this doesn't work so well, as you can see the price and open/closed labels are being cut off below:
I replicate the same code to determine the height of the cell. In this case, the "cell" is the primary horizontal stackView, which has an embedded image stackView and info stackView (same as in the tableView). My print statements indicate the height of the horizontal stackView is increasing as required, but it's pushing the content down rather than increasing the horizontal stackView upwards. That stackView is pinned to right, left and bottom of the superview, so theoretically that should not occur.
This code sets my horizontalStackView height:
func setStackViewHeight(heightForCellWithLocationName name: String) -> CGFloat {
var size = CGSize()
if let font = UIFont(name: ".SFUIText", size: 17.0) {
let fontAttributes = [NSAttributedStringKey.font: font]
size = (name as NSString).size(withAttributes: fontAttributes)
}
let normalCellHeight = CGFloat(96)
let extraLargeCellHeight = CGFloat(normalCellHeight + 20.33)
let textWidth = ceil(size.width)
let labelWidth = ceil(nameLabel.frame.width)
if textWidth > labelWidth {
print("***********EXTRA LARGE CELL HEIGHT****************")
return extraLargeCellHeight
} else {
print("***************NORMAL CELL HEIGHT*****************")
return normalCellHeight
}
}
And I use this code in my function that populates the stackViews with data:
horizontalStack.frame.size.height = setStackViewHeight(heightForCellWithLocationName: location.name)
if horizontalStack.frame.height == 116.33 {
infoStackView.frame.size.height += 20.33
}
What am I missing?