0
votes

I ran into the following problem. I have a custom class UITableViewCell with a few UIViews inside, which act as container for chartviews. The problem is if I do the following:

self.upperLeftChart = XYPieChart(frame: CGRectMake(0, 0, self.upperLeftContainer.frame.width, self.upperLeftContainer.frame.height))
self.upperLeftContainer.addSubview(self.upperLeftChart)

The Chartview is as big as the whole UITableViewCell instead of the size of my container. If I print the size of my container

NSLog("\(self.upperLeftContainer.frame.width) / \(self.upperLeftContainer.frame.height)")

it prints: 320.0 / 568.0 which is wrong. My container is about a quarter of the whole cell.

I guess it hast something to do with my Autolayout + Constraints. If I set the size to 120x120px hardcoded, it works nicely.

Any trick to get the real width and height of UIView arranged with autolayout and constraints?

1
Btw, what is the size of upperLeftContainer?Beraliv

1 Answers

1
votes

What you need to do is to override

override func layoutSubviews() {
    super.layoutSubviews()
    self.upperLeftCart.frame = CGRectMake(0, 0, self.upperLeftContainer.frame.width, self.upperLeftContainer.frame.height)
}

That way it will change frame of the view every time cell resizes