views:

39

answers:

2

I'd like to place an image behind the tableView in my UITabBarController moreNavigationController. I have tried inserting a subview like so when first setting up the TabBar:

UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background3.png"]];
[self.tabBarController.moreNavigationController.topViewController.view insertSubview:imageView atIndex:0];

But this places the image over the top, presumably because the tableView isn't there at the time. Is there a better time when I can call this in order to have it work properly, or an easier approach?

A: 

Besides all the dotty mess here, you can use UIView's bringSubviewToFront: and sendSubviewToBack: to organize your subviews. Basically this should help, although if you have more subviews you will need to play around with it a little bit:

[self.tabBarController.moreNavigationController.topViewController.view addSubview:imageView];
[self.tabBarController.moreNavigationController.topViewController.view pushSubviewToBack:imageView];
//or [self.tabBarController.moreNavigationController.topViewController.view bringSubviewToFront:tableView];
Estarriol
I think you mean 'sendSubviewToBack', and I'm afraid that doesn't make any difference. I think it is because at the time I am calling the code (at the end of applicationDidFinishLaunching), the controller hasn't been setup yet?
alku83
+1  A: 

With some assistance from this question, I figured out how to do this. Basically, the viewController in the moreNavigationController is a single TableView, so adding a background image won't work. What I need to do was to create a new view, add the background image, and then add the moreNavigationController view on top of that. I did this by overriding viewDidLoad in a subclass of UITabBarController, but I expect it could be done elsewhere as well.

- (void)viewDidLoad {
    [super viewDidLoad];

    UINavigationController *moreController = self.moreNavigationController;

    if ([moreController.topViewController.view isKindOfClass:[UITableView class]]) {
        UIView* newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,367)];

        UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background3.png"]];
        imageView.opaque = NO;
        imageView.alpha = 0.4;
        [newView addSubview:imageView];

        moreController.topViewController.view.backgroundColor = [UIColor clearColor];
        moreController.topViewController.view.frame = CGRectMake(0,0,320,367);
        [newView addSubview:moreController.topViewController.view];

        moreController.topViewController.view = newView;
    }
}

You could probably be smarter with the frame sizes, etc, but this works for me. Hopefully it helps someone else too.

alku83