Benches Uncategorized

10

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

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

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!

10 replies

Sign in or join Beachball Help to reply.

  1. 0

    Ryan Joseph

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    *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...

  2. 0

    jerbeers

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Try setting the frame after you add the table to the navigation controller. If it thinks the table is "full screen" and sets the frame as such, it won't matter what you are setting it to before, but might matter if you set it after. Just a hunch...

  3. 0

    just.do.it

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi, i'm not quite sure, but i've the strong feeling, that an UINavigationcontroller is only ment to handle full-screen views .

    "The navigation controller resizes and positions your views depending on where the navigation bar is and whether a toolbar is displayed." +(UINavigationController Class Reference)+

  4. 0

    applehund

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    in your loadView method, set the view to be a regular UIView and then add the UITableView to that:


    - loadView {
    view = [UIView new]; // this view will take up the full screen
    UITableView *table = [UITableView etc];
    table.frame = CGMakeYourOwnRect();
    [view addSubview:table];
    }

    hth

  5. 0

    crinale

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  6. 0

    Aral

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    A quick note that the above worked for me on viewDidAppear
    not viewWillAppear.

    By the way, thank you so much for sharing this. It's been kicking my butt for the better part of today 🙂

  7. 0

    saraoneal

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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!

  8. 0

    hams:)

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I tried using both the viewWillAppear and viewDidAppear solutions, and while the latter seems to have worked, elements that should appear beneath the UITableView still cant be seen. Any ideas?

  9. 0

    eduyanez

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    mate, THANK YOU, no really, T-H-A-N-K Y-O-U

  10. 0

    Arlito Tamayo

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    @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!