2

I want to have a UITabBarController, which is has an alpha of 0.5, whose transparency allows you to see a view in the background. The background view has to be accesible and changeable.

I'm able to add the background using this technique: https://gist.github.com/1157542

It's a Category which adds a subview to the UITabBarController, and sends the subview to the back. However, because it's a category, I can't make the subview a property. So I can't really access it.

Is there a way to make this background view more flexible and accessible? So I could, for instance, add other subviews to it easily from any of the tab bar controller's view controllers?

Cezar
  • 55,636
  • 19
  • 86
  • 87
cannyboy
  • 24,180
  • 40
  • 146
  • 252

3 Answers3

1

Instead of a category, you should subclass UITabBarController. This will allow you to have finer control over the object. Here's an example of a subclass.

// MPCustomTabBar.h
@interface MPCustomTabBar : UITabBarController
- (void) setBackgroundImage:(UIImage *)image;
@end

// MPCustomTabBar.m
@interface MPCustomTabBar

- (void) setBackgroundImage:(UIImage *)image  {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
imageView.backgroundColor = [UIColor colorWithPatternImage:i];  
[[self view] addSubview:imageView];
[[self view] sendSubviewToBack:imageView];
[[self view] setOpaque:NO];
[[self view] setBackgroundColor:[UIColor clearColor]];
[imageView release];
}

@end

Now you can do all the customization you want, alloc and init your new subclass by something like this:

MPCustomTabBar *bar = [[MPCustomTabBar alloc] init];
skram
  • 5,314
  • 1
  • 22
  • 26
  • 1
    My understanding is that subclassing UITabBarController is not allowed by Apple. http://stackoverflow.com/questions/9374213/alternative-to-subclassing-uitabbarcontroller – cannyboy Jun 16 '12 at 04:21
0

A solution to my problem might be simply this..

In my AppDelegate, just before [self.window makeKeyAndVisible];

self.theImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"]];
[self.tabBarController.view insertSubview:self.theImage atIndex:0];

I can then easily change this image, in any of the tab bar controller's view controllers, like:

AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.theImage.image = [UIImage imageNamed:@"image2.png"];

.. no categories required.

cannyboy
  • 24,180
  • 40
  • 146
  • 252
0

Here's what worked for me:

  1. Make TabBar transparent (either by Storyboard or programmatically) by setting the Tint- and Background to Clear Color and Opaque to No.

  2. The black background color is actually the window's colour. Assign the image you want to use as the background to the window itself:

    UIImage *i = [UIImage imageNamed:@"main_background.png"]; UIColor *c = [[UIColor alloc] initWithPatternImage:i]; [self.window setBackgroundColor:c];

E3G
  • 479
  • 5
  • 6