How to set the background of a UITableView to an image? - iphone

I am using a UITableViewController which uploads a table. I have a Nib File with UITableView in it.Now I want to set the background of the tableView either from interface builder or from the TableViewController to an image.
How to do that.
OK so I created an UIImage in my controller. Now when I add where do I need to add it.
When I try adding it and set the color of tableView to clearColor, it just shows me the Image and not the table although I make sure that image is being sent to back of all views.
Guys Please note that I am not dealing a UIView and adding a tableView as its subview But I am dealing with a UITableView .

Place a UIImageView behind the UITableView, then do this:
tableView.backgroundColor = [UIColor clearColor];

Somehow playing around I was able to find out a way.
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];

self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"parentViewBackground.png"]];
the image size should be
2x - 640*960
1x - 320*480
the size smaller than above, it will be tiled.

The backgroundColor = [UIColor colorWithPatternImage: image] method rkbang outlines works great. Here is another method to achieve the same effect by adding a new view to the parentViewController. This would be useful if you want to mask other contents the parentViewController might have.
In your UITableViewController subclass, in -viewDidLoad insert:
//make a cool background image
self.tableView.backgroundColor = [UIColor clearColor];
UIImage *patternImage = [UIImage imageNamed:#"backgroundImage.png"];
UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:patternImage];
[self.parentViewController.view addSubview:backgroundImageView];
[self.parentViewController.view sendSubviewToBack:backgroundImageView];
//release the background image and image view (the backgroundImageView is still retained by the parentViewController)
[patternImage release];
[backgroundImageView release];
You may wish to keep track of the backgroundImageView so you can remove it or replace it. The code example above leaves it to the parentViewController to manage the new view. If you're loading and unloading this UITableView, you'll be leaking these UIImageViews with every load unless the parentViewController is releasing them at the same rate somehow.

What you can do is set the backgroundColor property of the tableview to [UIColor clearColor] and have a UIImageView of the same size and position underneath your TableView to show your background.

TableView properties include background color. Set this to clearColor; and put the image behind it. (In IB I think you'll have to delete the tableview add an image and then add your tableview back again)
if you're subclassing UITableViewController you get a tableview for free, no ivar or nib required, however since you're using a background I would stick with IB.

You can use the table-view's backgroundView. This worked for me.
[self.tableView setBackgroundView: [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"????.png"]]];
where ????.png is your background image.

Related

How to add custom image to a UITableviewController in Xcode?

I am new to Xcode, and I am currently building my first app. I've searched long and hard to try and find the proper tutorial but I can't find one. I am looking for a way to be able to insert a custom background in my UITableViewController. I can change the color, but that is it. What I am looking to do is set my PNG image behind the static cells I've created, and drop the opacity on those cells so the custom image comes through. Can someone please give me a way to do this either through the IB (storyboard) or through the coding. Much appreciated!! Thanks in advance!
I think the easier and correct way is to:
[tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]]];
You can include this code in viewDidLoad to make it work.
for cell's background :, do this in willDisplayCell:atIndexPath: method
UIImageView *cellImageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:#"image.png"]]];
CGRect Frame = cell.backgroundView.bounds;
cellImageView.frame = newFrame;
[cell.backgroundView addSubview:cellImageView];
for tableview background , do this in viewDidLoad: method
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BackgroundPattern.png"]];
UIImageView *ivwCellBackground=[[UIImageView alloc]init];
[ivwCellBackground setImage:[UIImage imageNamed:CELL_BACKGROUNGIMAGE]];
cell.backgroundColor=[UIColor clearColor];
[cell setBackgroundView:ivwCellBackground];
[ivwCellBackground release];
ivwCellBackground=nil;

How do you access tableView in Master-Detail storyboard template?

I'm using storyboarding and the Master-Detail Core Data template in Xcode4.
I want to set the background image for my tableView on the master view controller.
To do this was simple for the iPhone, I just did this in viewDidLoad of the MasterViewController.m:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
However, this doesn't work for the iPad; the default background is being shown instead in the master tableView.
I wouldn't think this was a splitViewController issue; from the perspective of the MasterViewController object, he should always have the same tableView, right? In fact, I know this is the case, because this code works on both iPad and iPhone, a line above the "backgroundColor" assignment, and this is using the same "self.tableView" object:
self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"separator.png"]];
This is what it looks like on the iPhone, with both the separator and background image working (sorry, can't post pictures inline yet):
http://i.imgur.com/G7djz.png
And, using the same code, on the iPad:
http://i.imgur.com/8L0ZK.png
What do I have to set in order to set the background image on the master tableView?
You need to make the background clear in interface builder. Then, you need to say self.tableView.backgroundColor = [UIColor clearColor]; in your viewDidLoad. Finally, put a UIView behind it with the appropriate background color. For some reason, the background stuff is overridden in the UITableView itself.
Aha! I found the secret sauce:
UIImage *backgroundImage = [UIImage imageNamed:#"foo.png"];
UIView *backgroundView = [[UIView alloc] init];
[backgroundView setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];
[self.tableView setBackgroundView:backgroundView];
The key is using [self.tableView setBackgroundView:...]instead of the property self.tableView.backgroundColor = ...

Using background images for UITableViewCells (grouped style) - corners not rounded

I'm trying to create a grouped style tableview which features cells with a background image. Both the tableview and the cells have been set up in Interface Builder.
Everything works, however the cells won't clip the background images corners, leaving the cell square. I've tried to enable "clip subviews", I've tried adding an UIImageView as a subview (as opposed to making the background view an imageview), I've both tried selecting a background image directly or connecting a seperate UIImageView to the cell's backroundView Outlet - to no avail.
I've tried setting up the cell programmatically, too - doesn't work. It seems the only thing that will leave the corners rounded is selecting a background color (not an image) directly in Interface Builder, which is not what I want.
There are other questions on SO with related problems, none of which covered the use of background images however, so no help there.
Thanks alot for any insights..!
Have you tried _table.backgroundColor = [UIColor clearColor]; ?
FIXED: _cell.backgroundColor = [UIColor colorWithPatternImage:image];
The above did not work for me.
first set the background of your window to the color you want in your app delegate like this:
UIView *bgView = [[UIView alloc] initWithFrame:self.window.frame];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"main_background.png"]];
[self.window addSubview:bgView];
[bgView release];
Now if you just have one view controller just set its background to clear and your done. I had to to the next steps because I was popping a modal window with a table view and if the background of that view was clear it looked choppy coming up. So the code below fixed the choppy view and I still get the rounded corners because after the view has appeared I set the background to clear again and let the background from the window show through.
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"main_background.png"]];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.view.backgroundColor = [UIColor clearColor];
[self.tableView reloadData];
}

iPhone - UIView's backgroundColor - using a png with transparency?

I'm setting the background color of my UIView (which is within a UIViewController) as follows:
myView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"myTransparentBG.png"]];
I've even tried messing with [myView setOpaque:NO]; but the image doesn't appear transparent. It has a black background to it. Am I abel to fix this programmatically? Otherwise, how are we supposed to set a transparent background to our view?
This seems like a question that should have been asked before, but I couldn't find an answer.
When using a transparent pattern, after setting the backgroundColor property, you need to set opaque property to NO on both view and its layer. Here's a sample:
UIView* paperView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,700)];
paperView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"paper.png"]];
[paperView.layer setOpaque:NO];
paperView.opaque = NO;
I haven't ever actually done this, but here's what I'd try.
Set the UIView's .backgroundColor to [UIColor clearColor]. Then put a UIImageView containing your PNG as the "lowest" item on the UIView's stack of subviews. Make sure the UIImageView's background color is also clear.
Use this: myView.backgroundColor = [UIColor clearColor];
This will set it to a transparent background.

How do I set a background image for a grouped table view?

I've seen some iPhone applications that use a custom image as the background for a grouped UITableView, instead of the standard gray lines.
How is this achieved?
Here's what worked for me (and fairly simple once I figured it out ;)
1) Add a view in your app delegate and make it a subview of the window:
UIView *bgView = [[UIView alloc]initWithFrame:window.frame];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"screenBG.png"]];
[window addSubview:bgView];
[bgView release];
2) On each view controller .m file, under ViewDidLoad, set background color of that particular view to transparent (so the other bgView created above will show through):
self.view.backgroundColor = [UIColor clearColor];
And in my case, the view controller in step 2 was a tableviewcontroller. Looks great.
And BTW, doing the following in each view controller did NOT work well:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"screenBG.png"]];
So follow steps 1 and 2 above.
Hope this helps out,
Tbone
Try this
- (void) viewDidLoad {
[super viewDidLoad];
self.tableView.backgroundColor = [UIColor clearColor];
self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:#"wallpaper.png"]];
self.tableView.opaque = NO;
}
In another project (developed using 2.2.1) I did this by setting my UITableView's background opacity to 0%, and then simply layering a UIImageView behind it using Interface Builder. This allowed me to have a fixed background regardless of the table state. You can also set the background of the UITableView to be an image instead, but then the background scrolls with the table. (I don't have the code handy at the moment, but I got the tip a while back on the Apple developer forums).
Note that this can cause some performance issues. Apple discourages using transparency whenever possible because the GPUs on the pre-3GS models aren't particularly beefy.
You can use the +[UIColor colorWithPatternImage:(UIImage)] method like so:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];
the problem with colorWithPatternImage: is that you need to use "patterned" images, otherwise your image will be tiled randomly
this link has a simple solution, if you want all your views to have the same background
http://howtomakeiphoneapps.com/2009/03/how-to-add-a-nice-background-image-to-your-grouped-table-view/
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"SortByCategory_320x480.png"]];
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.backgroundColor = [UIColor clearColor];
Hope this will help. It won't display hideous translucent background behind the cells especially in case of Grouped UITableView.