UITableView with custom frame size within a UINavigationController... how?

I've seen similar questions asked all over the web, but so far not a single "real" answer has cropped up, so hopefully you folks will know the way to do this.

I have a main view that with a UIView subview, inside of which I am programmatically adding a UINavigationController and UITableView that is encapsulated in a +regular UIViewController+, to allow it to be pushed/popped onto the navigation controller. I'm essentially just trying to get a table+navcontroller combination that only takes up about half the screen height.

The problem isn't that it doesn't work, because I have no problem getting the table and navigation controller working together as expected. The problem is that no matter what I do or try, I cannot get the table view to be smaller (in height), than full-size, meaning that when contained in this ~200px navcontroller-in-subview, there are a large number of rows the user cannot select because attempting to scroll to them immediately "bounces" back. You can see that the rows are there when you "pull" the scroll down, but can never get it to stay.

As it appears, the table view is too large in height. I've gone as far as setting the table to a height of 10, to no avail. Autoresize masks have no effect.

Please note that I'm NOT using an instance of UI TableViewController, but rather the plain vanilla UIViewController, because creating an instance of the former automatically creates the underlying UITableView, which was always sized to large, as well. I had some advice that I should attempt creating the table and view controller independently, add the former to the latter and then push the view controller onto the navigation controller. Again, this does work, but the size is all out of whack.

Finally, in quick psuedo-code, this is essentially what I'm doing:

(this is found in MainViewController::viewDidLoad)

CGRect frame = subviewContainer.frame;
frame.size.height = 10;
frame.origin.x = 0;
frame.origin.y = 0;
UITableView* table = [[UITableView alloc] initWithFrame:frame];
table.autoresizesMask = UIViewAutoresizingNone;
UIViewController* tVC = [[UIViewController alloc] init];
tVC.view = table;

UINavigationController* navC = [[UINavigationController alloc] initWithRootViewController:tVC];
[subviewContainer addSubview:navC.view];


Thanks for any tips you might have: this is quite the frustrating problem!

MacPro, Mac OS X (10.5.5), MBP 1st-gen

Posted on Oct 21, 2008 4:43 PM

Reply
10 replies

Oct 21, 2008 4:59 PM in response to Ryan Joseph

*A quick update* I've tried all the techniques I've seen here and elsewhere on the web to no avail, but I did find one interesting thing: if I eschew the UINavigationController altogether (which I'm not yet willing to do for production, but as an experiment), and add the table view as a direct subview of the enclosing view I mentioned, the frame size given is respected. The very moment I re-introduce the UINavigationController into the mix, no matter if it is added as a subview before or after the table view, and no matter if alloc/init it before or after the table view is added as a subview, the result is the same as it was before.

I'm beginning to suspect UINavigationController isn't much of a team player...

Nov 3, 2008 3:24 PM in response to Ryan Joseph

i got the table view to resize by setting its frame in viewwillAppear:animated

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

// set the frame size
CGRect frame = self.view.frame;
frame.size.height = 313;
self.view.frame = frame;
}

it seems when you set it at viewDidLoad or some other place, it'll get resized when it gets pushed on the navi controller.

Jun 17, 2009 8:14 PM in response to Aral

I have an app that is absolutely kicking my butt. I have a UITabBarController that contains a UINavigationController that contains a custom UIViewController. When the user rotates the iPhone, I want to hide the navigation bar, hide the tab bar and swap out a UIImageView in the custom view controller and I want the image to be full-screen. I've figured out how to get rid of the navigation and tab bars, but making the UIImageView full-screen is what's killing me. When the image shows up, it is the size it would be if the tab bar was visible. In the didRotateFromInterfaceOrientation method, I hide the tab bar by doing:

tabBar.hidden = YES;

I've tried resizing the UIImageView by doing:

img.frame = CGRectMake(0.0, 0.0, 320.0, 480.0);

but that just screws up the image even more. I tried resizing the tabBarController's view, and I've tried resizing the current view, but alas to no avail.

Can anyone help me not be stupid?

Thanks!

Aug 27, 2009 11:14 PM in response to applehund

@applehund

Just wanted to thank you as your suggestion worked perfect for what I was trying to do (I have a custom button bar sitting between a UINavigation Bar and a UITableView ). I could get the table to "reposition" below the button bar using the logic of re-setting the frame within viewDidAppear, but only after it appeared right under the nav bar temporarily (so that the first row of the table was partially hidden by my button bar for a split second). Now the table sits under the button bar like it always belonged there. 😉 Thanks!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

UITableView with custom frame size within a UINavigationController... how?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.