Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView not resizing correctly on iPad only

I have a static UITableView with 3 rows in 1 section. Each row contains a UITextView that should resize based on the content. Each of the UITextView objects has scrolling disabled to force it to grow. Each is pinned on all 4 sides to the cell's contentView and has no other constraints.

When I run on iPad (real or sim) the cell resizes to a single line of text regardless of the contents of the UITextView. When I examine the view hierarchy in the debugger, I see that the UITextView height is being set to 27.5 even though the content view height is 183.5. This appears to be a result of the height constraint on the UITableViewCellContentView which is limiting the cell height. This is not a constraint that I set. My table view controller does not have a heightForRow or cellForRow method because it is a static table view and uses auto layout for everything. I don't know how this constraint is being added.

enter image description here

Here is the exact same hierarchy with the exact same contents at the exact same point on when run on an iPod touch. Note that the UITextView height and the content height are the same this time. In this case, the UITableViewCellContentView constraint on height is large enough to accommodate the UITextView height.

enter image description here

In both cases, this is the same source executing and displaying the same content. That content contains short text lines with carriage returns, which is why it should size to the same content size in both screen sizes. The UITextView wants to size its height to 183.5, but on the iPad the cell height is being unnecessarily constrained by a constraint that is generated by iOS.

Here is the code for viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

    //shrink the uiswitch a little bit
    self.displayRegexCharsSwitch.transform  = CGAffineTransformMakeScale(0.70, 0.70);

    //these next 2 lines set up the table to automatically resize based on content
    self.tableView.estimatedRowHeight = 100.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    //next line hides the blank cells
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    //these are needed on iPad or rows with accessory view don't resize
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

Again, there is no heightForRow or cellForRow in the table view controller due to using static table view.

I'm baffled. Why is the UITextField reporting a different content size on the iPad than on the iPod if the content is the same and the code is the same? It is clear from the hierarchy view that the UITextField internally knows that its height is 200, but contentSize is returning 27.5. This is a static table view, so there is no heightForRow method and no cellForRow method. I am depending on auto layout to manage all of the layout - which it does correctly on iPad.

EDIT

FYI - The design is based exclusively on Any/Any size class.

enter image description here

like image 752
Chuck Krutsinger Avatar asked Jun 08 '15 19:06

Chuck Krutsinger


1 Answers

Found it! I ported the layout to a new project to isolate the issue from other things going on in the project. The layout problem did not occur in the new project. So I compared overall project settings and I noticed that the new project had a deployment target of iOS 8.3 and the original project had a deployment target of iOS 6.0. When I set the new project deployment target to iOS 6.0, the layout issue showed up. Same for iOS 7.0. However, for iOS 8.0 and above, the problem disappears. Time to force my users to upgrade to iOS 8.

enter image description here

like image 122
Chuck Krutsinger Avatar answered Oct 20 '22 07:10

Chuck Krutsinger