Ben Williams
Ben Williams

Reputation: 4695

Background image for MoreNavigationController

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?

Upvotes: 1

Views: 1690

Answers (3)

gwdp
gwdp

Reputation: 1212

Now you can acess backgroundView property from UITableView subclasses .

    UIViewController *moreViewController = tabBarController.moreNavigationController.topViewController;

    img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BG_MORE+1.png"]];

//Got some crashs in initialization !! Need to check .
if ([moreViewController.view isKindOfClass:[UITableView class]]) {
    UITableView *moreTableView = (UITableView*)moreViewController.view;
    [moreTableView setBackgroundView:img];
}

Upvotes: 1

Ben Williams
Ben Williams

Reputation: 4695

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.

Upvotes: 3

Mayjak
Mayjak

Reputation: 1497

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];

Upvotes: 0

Related Questions