Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot change height of UITableView

In the Apple iPhone Clock App, when a user adds a new alarm a modal view pops up. I am trying to create a UI similar to that.

I currently have a UITableViewController as the root view controller of a UINavigationController.

I also have a UIDatePicker added as a subview to the UINavigationController:

[self.navigationController.view addSubview:mydatePicker];

However, I have about 10+ rows in my UITableview (that of the UITableViewController) and as soon as I added the UIDatePicker I cannot scroll to view all the cells.

I realized that the UITableView size is the same size as it was before I added the UIDatePicker, and therefore I would need to change its size in order to be able to scroll to see all the table cells.

I have tried several things in order to change its size, all to no avail. In the code below I arbitrarily chose 50 for the new height.

First, tried changing the bounds height:

CGRect bounds = [self.tableView bounds];
[self.tableView setBounds:CGRectMake(bounds.origin.x,
                                     bounds.origin.y,
                                     bounds.size.width,
                                     50)];

Then tried to change the frame height:

CGRect tvframe = [self.tableView frame];
[self.tableView setFrame:CGRectMake(tvframe.origin.x,
                                    tvframe.origin.y,
                                    tvframe.size.width,
                                    50)];

Then after googling some more i tried changing the contentSize height:

CGSize thesize = self.tableView.contentSize;
thesize.height = 50;
self.tableView.contentSize = thesize;

None of these appeared to have any effect on the size of the UITableView. I still could not scroll to see all the cells.

I later tried some of the same methods as above but on the UINavigationController instead of the UITableView. I didn't have much luck with this either.

As a last resort, I tried to change the size of the UITableView in the Storyboard editor. I could not figure this out either.

Thanks for any help you can provide.

like image 778
dtmland Avatar asked Feb 10 '13 05:02

dtmland


2 Answers

From the UITableViewController class reference, it:

creates an unconfigured UITableView object with the correct dimensions and autoresize mask

And a quote from the Table View Programming Guide for iOS which specifically addresses this behavior:

Note: You should use a UIViewController subclass rather than a subclass of UITableViewController to manage a table view if the view to be managed is composed of multiple subviews, only one of which is a table view. The default behavior of the UITableViewController class is to make the table view fill the screen between the navigation bar and the tab bar (if either are present).

If you don't want the table view controller setting the size of your tableView, then you need to use a UIViewController instead. See the link that I posted above to the Table View Programming Guide for iOS for other things to consider when going this route.

like image 65
lnafziger Avatar answered Nov 21 '22 23:11

lnafziger


I had same problem as you mentioned. I was unable to change the height of tableView of UITableViewController. I tried changing frame and centers of self.view and self.tableview. But failed. Then I solved it by a trick, changing tableview's contentInset.

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 44.0f, 0); // here you can replace 44.0f with your height
like image 28
Nitesh Borad Avatar answered Nov 21 '22 22:11

Nitesh Borad