Question

I have a simple UITableViewController subclass with a few UITableViewCells. I'm using the imageView and textLabel on each cell.

When I rotate the device to/from portrait/landscape the frame of the textLabel changes. The change is fine for portrait to landscape rotation. However when rotating back to portrait from landscape the frame changes to an "incorrect" value (text is justified inappropriately).

How can I solve this?

I had a similar issue with the imageViews' frames changing. I solved this by subclassing UITableViewCell and setting the frame to the correct value in layoutSubviews. Here there is only one CGRect value that I need to remember (and I just hardcode it).

The textLabel is different: I'd need to keep track of two frames (one for each orientation) and I think the frame values would depend on the textLabel's text. So adding two CGRect instance variables to my UITableViewCell subclass is an option. However I imagine there's a simpler solution.

Was it helpful?

Solution

Problem was cell.textLabel.numberOfLines = 0; in cellForRowAtIndexPath. Setting numberOfLines to three solved the problem.

I also had to set self.textLabel.frame.origin.x to a hardcoded value in layoutSubviews.

Not sure if I completely understand what's happeing here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top