views:

234

answers:

3

When I add a subview to my UITableViewController, it seems to be underneath the tableview. I may be loading my subview incorrectly, or calling addSubview in the wrong place. The subview I'm referring to is the red area above the tabbar that also contains the "Click me" button:

screen shot

You can see that the cell lines kind of overlap. Here is where I'm adding the subview in my TableViewController:

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect hRect = CGRectMake(0, 170, 320, 480);
    HomeScreenOverlayView *homeView = [[HomeScreenOverlayView alloc] initWithFrame:hRect];
    [self.tableView addSubview:homeView];
    [homeView release];
}

Any ideas? Thanks!

A: 

You can use - (void)sendSubviewToBack:(UIView *)view or - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index. addSubview always puts the new view in front.
EDIT: Sorry, misread the question, it looks like you want the subview to be in front.

eman
+1  A: 

When you call addSubview, it is probably the first view added to the table view. Later, all the cells and support views will be added over your view.

The best thing to do is create an empty view and add both the table view and overlay view to it, making sure the overlay view is above the table view.

Views serve the 3 roles of drawing, interaction and layout. It is fine to have a view that only fills one of those roles.

drawnonward
That worked for me, thanks.
Banjer
A: 

I have had this issue myself and resolved it by not adding a view to the table, but rather adding the view to the table's superview.

UIView *viewToAdd = [[UIView alloc] initWithFrame: self.view.frame];
[self.view.superview addSubview: viewToAdd];

This is particularly useful when you want to mask the entire table (e.g. loading screens).

N.B. I will usually add this to viewWillAppear: in the view's lifecycle.

ddawber