Eric
Eric

Reputation: 265

Proper sizing of UIViewController's view

After dealing with this issue so many hours without any luck, I'm trying different approach by creating views programmatically in my UIViewController's loadView. I have UIToolbar and UITableView in my UIViewController but I have trouble with the sizing when adding the view as another UIView's subview. Here's what I got so far:

In my custom UIViewController' loadView:

{

    [super loadView];

    //this doesn't seem right!?!?
    self.view.frame = CGRectMake(0, 0, 400, 300);

    //create the Tool Bar
    self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 45.01)];

    [self.toolbar setBarStyle:UIBarStyleDefault];
    [self.toolbar setAutoresizesSubviews:TRUE];
    [self.toolbar setAutoresizingMask:(UIViewAutoresizingFlexibleWidth)]; 

    // create the array to hold the buttons, which then gets added to the toolbar
    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:1];

    //create buttons
    UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
    bi.style = UIBarButtonItemStyleBordered;
    [buttons addObject:bi];

    //add buttons to the toolbar
    [self.toolbar setItems:buttons animated:NO];

    //add toolbar to the view
    [self.view addSubview:self.toolbar];


    //create UITableView
    //a better way setting to set the frame!?!?
    UITableView* listTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 
                                                                           self.toolbar.bounds.size.height, 
                                                                           self.view.bounds.size.width, 
                                                                           self.view.bounds.size.height - self.toolbar.bounds.size.height) 
                                                          style:UITableViewStylePlain];
    [listTable setAutoresizesSubviews:TRUE];
    [listTable setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];
    [listTable setDataSource:self];
    [listTable setDelegate:self];

    self.table = listTable;
    [self.view addSubview:listTable];
}

In rootViewController's loadView():

{
    [super loadView];

    controller = [[CustomViewController alloc] init];
    [self.customView addSubview:controller.view];
    [controller.view setAutoresizesSubviews:TRUE];
}

You can see the screenshot here: https://plus.google.com/u/0/photos/112841433450419935389/albums/5693241484889252817/5693241483569881554

I'm new with this, other than getting the sizing correct, am i on the right track?

Many thanks.


Edit:

I'm trying to create a view like this

This one is close to what I'm trying to do except that I will have two UITableView and no segmented buttons. https://plus.google.com/u/0/photos/112841433450419935389/albums/5693241484889252817/5693260341279558818

Upvotes: 0

Views: 1731

Answers (2)

Jonah
Jonah

Reputation: 17958

You've made some assumptions that are contrary to the way UIViewControllers are expected to be used and I think that mismatch is at the root of your confusion.

UIViewControllers do not normally manage the size of their own root view. Their view is resized by it's container. That means that the window or a container view controller like a UINavigationController (or a custom container view controller using the iOS 5 APIs) manages the frame of the controller's view.

Attempting to adjust a controller's frame is -loadView is unlikely to have an effect because the view's frame may be reset when the view is later added to the window or parent controller's view. Similarly a controller shouldn't adjust it's own view's frame after the view has been loaded because that might compete with its parent view's -layoutSubviews behavior.

Instead I think you want a view controller whose view contains both the UITableView and UINavigation bar you want to display. As @VinceBurn suggested it sounds like you want behavior a UINavigationController already provides.

Upvotes: 0

Vincent Bernier
Vincent Bernier

Reputation: 8664

From what I can deduct of your code you want a UITableView to fill all the screen except for a bar on the upper part of the screen. (Usually a UIToolBar is on the bottom of the screen)

If you take a UITableViewController and put it as the rootViewController of a UINavigationController you will have a lot of nice behaviour and sizing for free.

In a UINavigationController the bar at the top of the screen is the navigationBar and you can have a toolBar at the bottom with this call if you want :

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated

And the View Controller Programming Guide for iOS is a good place to look into.

Upvotes: 0

Related Questions