add background image to UITableViewController in Navigation based app - iphone

I have a navigation based app where I push UITableViewControllers onto the stack. I would like to add a background UImage to all of my UITableViewControllers. Not a UIColor, but an UImage. I know how I can do this using a Nib file and setting the UITableView itself to have use [UIColor ClearColor], but I don't want to go through all my UITableViewControllers and change them to using Nib files, etc.
I also found this solution which would be great if I was just using a single tableviewcontroller in my app. I think there might be a way to make this work, by adding a subview "below" my table view that is created by default in a UITableViewController?
Any suggestions would be great.

It's a little different in a navigation based app: just change the background of the navigation view that each table view is sitting on. Placing the following code in viewDidLoad of each UITableViewController works:
self.navigationController.view.backgroundColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:#"myImage.png"]];
self.tableView.backgroundColor = [UIColor clearColor];
But you may only need to do it once, at the top level of the navigation controller, rather than in each tableview controller (although you'll still have to set each background to clear).

On iOS6 use this:
UIImageView *boxBackView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TextureBoxboard.jpg"]];
[self.tableView setBackgroundView:boxBackView];

If your class is a UIViewController subclass then you can do it like this:
[self.view setBackgroundColor:
[UIColor colorWithPatternImage:
[UIImage imageWithContentsOfFile:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
#"background.png"]]]];

UIImageView *backgroundView =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
backgroundView.frame = CGRectMake(0,
0,
self.navigationController.view.frame.size.width,
self.navigationController.view.frame.size.height);
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
[self.navigationController.view insertSubview:backgroundView atIndex:0];
[backgroundView release];
self.tableView.backgroundColor = [UIColor clearColor];
As Gorm said
only need to do it once, at the top level of the UINavigationController

The answer given my Madhup is the correct answer. UITableViewController is a subclass of UIViewController, so adding that to your UITableViewController's viewDidLoad method works great.

As of iOS 3.2, -[UITableView setBackgroundView:] exists, which may be easier than some of the other proposed solutions going forward.

Related

TableView with background image

I want to have an image behind my tableview, i added a UIImage behind and set the tableview background to clear, in IB it shows the image behind the tableview but when i run it i get a black background, anybody can help me with this?
take off the image and add this code to your viewDidLoad:
UIView *patternView = [[UIView alloc] initWithFrame:self.tableView.frame];
patternView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
patternView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.backgroundView = patternView;
this is the way to add a fixed image as a background of a tableview.
or you could do that without subclassing by simply assigning the tableview's backgroundView property in your main controller class...
Try following code in -(void)viewDidLoad method
self.tblView= [[UITableView alloc] initWithFrame:CGRectMake(17,110, 280.0, 265) style:UITableViewStyleGrouped];
self.tblView.delegate=self;
self.tblView.dataSource=self;
//self.tblView.backgroundColor=[UIColor clearColor];
self.tblView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"img3.png"]];
[self.view addSubview:self.tblView];

UIStoryboard application and common background image

I have an iOS application which uses an UIStoryboard to control its flow. I would like to have all my views defined in the UIStoryboard to all share a common background. Is there a way I can do this without having to add an UIImageView control to each View?
I have tried this below but it causes my application crash with a stack overflow error:
-(void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"MyBackgroundImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
}
Is there a better way to do this? What is the best way to handle this kind of theming in iOS applications?
Subclass UIViewController and override -viewDidLoad to create your image and set it as the background of the view. Now make the view controllers that require this background image subclasses of your custom view controller instead of UIViewController.
In your ViewDidLoad:
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
imgView.image = [UIImage imageNamed:#"image.png"];
[self.view addSubview: imgView];
[self.view sendSubviewToBack:imageView];
Should do the trick.
It turns out that the code I posted above is working perfectly. My solution was the same as Mark Adams' suggestion: Subclass UIViewController, override -viewDidLoad, create and set the imageView, and use the new subclass as my viewController.
I think I may have inadvertently set my new subclass to an incorrect control in Interface Builder which caused my initial solution not to work correctly.

Adding backgroundimage to my app

I am using this code to add a background image to my tableview
myTable.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]] autorelease];
on the internet they say this code works, but it's not working for me
can someone help me please?
if its a UITableViewController, as I believe it is, do this:
[myTable setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
or you can do (on a non-table view controller)
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
or on viewDidLoad you can do:
UIImageView *backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]] autorelease];
[self.view addSubview:backgroundView];
but thats a bit messy - I prefer to use setBackgroundColor:
The problem is that backgroundview for tableview is a UIView, not a UIImage. So you need to create a view, easiest way is to use a UIImageView. For example this would put your splash screen image as the background view for the table in a UITableViewController...
UIImage *backImage = [UIImage imageNamed:#"default.png"];
UIImageView *backImageView = [[UIImageView alloc] initWithImage:backImage];
self.tableView.backgroundView = backImageView;
The methods mentioned above are pre the introduction of tableview property backgroundview in 3.2
edit - dear god I was obviously having a coffee deficit today :) The op is indeed using a UIImageView! The only thing I can think is that he is targeting a pre 3.2 platform...

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?
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.
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];
}
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];

iPhone Fixed-Position UITableView Background

I'm building an iPhone app without the use of Interface Builder. I have set the background of a grouped UITableView in the following manor:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"groupedBackground.png"]];
I'm trying to fix this background image so that it doesn't scroll with the table cells. Does anyone know how to do this?
We find that it works exactly as you ask, if instead of using backgroundColor, you assign to backgroundView, like so:
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"background.png"]];
The table cells then scroll on top of the background, which is stationary. (This has been available since version 3.2 of the SDK, I believe.)
You can achieve a stationary background using a pattern image as follows:
UIView *patternView = [[UIView alloc] initWithFrame:tableView.frame];
patternView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"groupedBackground.png"]];
patternView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tableView.backgroundView = patternView;
A little late but maybe for all the others looking for a solution. I could get that work by calling the parentViewController in the viewDidLoad method of the UITableViewController:
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
self.tableView.backgroundColor = [UIColor clearColor];
or with using just a background color:
self.parentViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
self.tableView.backgroundColor = [UIColor clearColor];
It took me a while to figure that out but now it works like a charm.
You can place an additional view below UITableView and set its background, so if UITableView is transparent - you'll achieve your goal - it will have correct background and it will not scroll.
use this below code in swift 3.0 to achieve constant background for tableview
self.tableView.backgroundView = UIImageView(image: UIImage(named: "background.png")!)