aherrick
aherrick

Reputation: 20179

iOS 7 Table View Landscape Cell Width

I've added a table view to a UI View Controller. When I rotate the phone into landscape mode the cell width isn't completely filling the view. See the below screenshot.

Cannot seem to find an elegant solution here.

enter image description here

EDIT: you can download my example of the landscape view not resizing here:

http://andrewherrick.com/spike/tableviewresize.zip

Upvotes: 3

Views: 2078

Answers (2)

Viraj Mantri
Viraj Mantri

Reputation: 1

It looks like apple iOS 7 orientation resize table view has some issue.

Using following into right place can resolve problem.

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows]
withRowAnimation:UITableViewRowAnimationNone];

Along with this call, if need one may also have to change width as per new orientation size in cellforRowIndexPath data source.

Upvotes: 0

Infinity James
Infinity James

Reputation: 4735

Add the UITableView using auto layout or with the appropriate autoresizing masks.

PROGRAMATICALLY

Auto Layout:

UITableView *tableView = [[UITableView alloc] init];
tableview.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:tableView];
NSDictionary *viewsDictionary = @{@"tableView" : tableView };

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|"
                                                                      options:kNilOptions
                                                                      metrics:nil
                                                                        views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|"
                                                                      options:kNilOptions
                                                                      metrics:nil
                                                                        views:viewsDictionary]];

Autoresizing Masks:

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:tableView];

INTERFACE BUILDER / STORYBOARD

Auto Layout:

Control drag from the table view to the superview and add these constraints:

Layout Constraints

Autoresizing Masks (No Auto Layout):

These should be the autosizing mask for the table view if you are using manual frame manipulation

Autoresizing Masks

NOTE

In all seriousness dude, I have given you as clear away as I can to size your view appropriately to make it flexible. If you struggle to understand this then I suggest you go back and look at a few tutorials for Beginning iOS Development. RayWenderlich is an amazing resource for everything you can think of when starting iOS.

UPDATE

Your test project with the added constraints. It literally took me about 5 seconds to do it. Look at the iPhone storyboard.

http://speedy.sh/2jDwY/tableviewresize.zip

Upvotes: 8

Related Questions