Question

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!

Was it helpful?

Solution

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:CGRectMake(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.

OTHER TIPS

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.

Create UIViewController subclass instand of UITableViewController Subclass.

insert UITableView instance.

in NIB simply drag and drop UIView

on top of that wier place the existing UITableVIew object.

set the size of the uitableview via nib or viewDidLoad method.

set the reference , dataSource and delegate via nib.

now its simply transfer the UIViewController class and the can change tableview size as you wish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top