views:

1578

answers:

2

I have a UITableView which is not being resized properly using autoresizeMask (in iPhone 3.0).

The UITableView is inside a UIViewController inside a UINavigationController inside a UITabBarController, all of which are being created programatically. The status bar is visible.

The code of the UIViewController is basically:

- (void)loadView {
    UIView* rootView = [[UIView alloc] init];
    self.view = rootView;
    [rootView release]; 
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-20-49-44)];
    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; table.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:table];
}

When created like this, the UITableView is slightly bigger than the available space. If I'm not mistaken, it's exactly 44 pixels bigger, the size of the navigation bar.

However, if I uncomment the commented line and comment the next line the size of the UITableView is exactly right. I would prefer to use autoresizingMask instead of manually calculating the size of the UITableView. What am I doing wrong?

Thank you in advance!

+1  A: 

The autoresizing mask will not help you with the initial size of the table view. The table view is created with the frame that you give it. The autoresizing mask defines the rules for resizing this frame relative to the parent view when the parent's frame changes.

So if I define a table that is 320x100 it will stay that size unless I change it explicitly or the parent view's frame changes.

Depending on the other views, you could do the calculation based on the other views held by the parent or by the parent's frame itself.

Colin Gislason
Thanks for the swift reply, Colin. The autoresizing mask IS doing something. The table view is being created with a 320x100 size, and yet it fills all the screen. I can change 100 to 200 and the size will be the same.
hgpc
And if you don't set an autoresizing mask but still set it to 320x100, it doesn't fill the whole screen?
Colin Gislason
That's right. With no autoresizing mask set the table view is sized according to the frame.
hgpc
Ok. Then the UITableViewController must be resizing the table. This questions provides evidence and maybe a solution: http://stackoverflow.com/questions/222956/using-a-uitableviewcontroller-with-a-small-sized-table
Colin Gislason
I'm not using a UITableViewController. I'm subclassing UIViewController.
hgpc
Sorry, I missed that. Something is going on that I can't explain then. Somewhere your table is being resized. You can experiment with the mask to verify it. If you set the frame to 100x100, and a mask that only resizes vertically it should still stay 100px wide.
Colin Gislason
If I set the frame to 100x100 and a mask that only resizes it vertically, it stays 100px wide. It seems that the only thing that is resizing the table is the autoresizingMask.
hgpc
That's where my answer comes in. The autoresizing mask doesn't do the autoresizing. It only defines the rule. Whatever resizes it (usually the parent UIView) is obeying that rule. When a view is resized, it resizes it's children, which then resize their children all the way down the hierarchy. The Apple documentation explains a bit: http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/autoresizingMask
Colin Gislason
A: 

The problem seems to be that I wasn't setting the frame of the root view in loadView. If you define such frame, and then define the frame of the subviews in relation to that frame, then the autoresize masks will correctly resize the subviews according to how the root view was resized by the framework.

For example:

- (void)loadView {
    UIView* rootView = [[UIView alloc] initWithFrame:GCRectMake(0, 0, 320, 480)];
    self.view = rootView;
    [rootView release]; 
}

- (void)viewDidLoad {
    [super viewDidLoad];

    table = [[UITableView alloc] initWithFrame:self.view.frame]; 
    table.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:table];
}

Thanks to Colin Gislason who pointed me in the right direction.

hgpc