UITableview background view colour -iphone - iphone

I have an UITableview controller without an XIB.
I set the background colour of the tableview by doing this:
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"MmyImage.png"]]];
My image is a gradient image, so eventhough the above code is ok, it redraws the image for each cell when the tableview goes to edit view, and it appears as lines which is not looking elegant.
I would like to set the tableviews background to clear colour and set the superview's colour to the image, so that the tableview transitions smoothly over the superview. However the below code does not work:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"myImage.png"]]];
[self.tableView setBackgroundColor:[UIColor clearColor]];
This makes the background completely white, I dont know why.
Help appreciated..thanks.
Edited:new code but this does not work as well:
UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]];
imgBg.frame = self.tableView.window.bounds;
[self.tableView.superview addSubview:imgBg];
//[self.view addSubview:imgBg];
//[self.tableView.window sendSubviewToBack:imgBg];
//[self.view bringSubviewToFront:self.tableView];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[imgBg release];

In a custom UITableViewController subclass, self.view will return the same as self.tableView. So what you're doing there is altering the same object. In the end, you get to see the UIWindow. So to get the desired result, you should do this –
[self.view.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"myImage.png"]]];
[self.tableView setBackgroundColor:[UIColor clearColor]];
UITableView has a backgroundView property. Use it.
aTableView.backgroundView = [[[UIView alloc] initWithFrame:aTableView.bounds] autorelease];
aTableView.backgroundView.backgroundColor = backgroundColor;

First in your xib file, add a parent view. add table view in that parent view. set parent view's background color to your desired background. then set your tableview's background color to [UIColor clearColor].
Check also, if your table view is a group table style or not? if it is group table style, then the cell have distinct background. Then you need to clear the background of each cell. The best way to do is,
create a blank UIView *bkview = [[UIView alloc] initWithRect:CGRect(0,0)];
set this
as your cells background view.
I hope this will work.

then your second code will be right, except:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"myImage.png"]]];
replace this:
UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]];
imgBg.frame = self.view.bounds;
[self.view addSubview:imgBg];
[self.view sendSubviewToBack:imgBg];
Edit
I think:
[self.tableView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]]; will helps, that is the only difference now.

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

How to add an image subview to a tableview programmatically

I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.

How to set UITableView background to image?

I've noticed that a number of apps have a custom background image for UITableView (or have set the background to a color.)
I can't find any API in the UITableView class to affect this, and my attempts in interface builder have failed (trying to set the color of the UIView)
Any suggestions? I've seen a third party app with pictures, so I know it can be done.
I just ran into the same question myself, but I found a way to do it that TomSwift touched on. As he mentioned, UITableView has a backgroundView property that is intended to do just what you're looking for. If you want to set the background color, you should instead use the backgroundColor property, as others have said. So:
// Set an image as the background of a UITableView called 'tableView'
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MyImage.png"]];
[tableView setBackgroundView:bg];
or:
// Set a solid color as the background of a UITableView called 'tableView'
// In this case I chose red
[tableView setBackgroundColor:[UIColor redColor]];
add this line in you code where you want to set background color for your image
[YourView(or imageView) setbackgroundColor:[UIColor
groupTableViewBackgroundColor]];
Thanks
UIImageView *image = [[UIImageView alloc] initWithImage:
[UIImage imagenamed:#"no_image.png"]];
[self.view addSubview:image];
UITableView *tableview = [[UITableView alloc] init];
[image addSubview:tableview];
tableview.backgroundcolor = [UIColor clearColor];
try this. it works for me.
CALayer *background = [CALayer layer];
background.zPosition = -1;
background.frame = self.view.frame;
background.contents = [[UIImage imageNamed:#"background.jpg"] CGImage];
[tableView.layer addSublayer:background];
you need to add an image view and set the table background color to clear color see this link for tutorial
Set the backgroundView property of the tableView. You can set it to a view where you have set a custom backgroundColor, or you can set it to a UIImageView where you've set an image.
alternative solution of GhostRider
No coding
Make UiImageView by the interface builder,
put your image in resources.
set it in UIImageView Image property by the use of Inspector.
select table view and set it background color to clear(for this make opacity to 0 by using attribute inspector).
This is how I did it. Probably not the best way, but works well enough.
UIImage *bg = [UIImage imageNamed:#"bg"];
[bg drawInRect:self.view.window.frame];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:self.view.window.frame];
bgView.image = bg;
activityTable.backgroundView = bgView; //activityTable = UITableView

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...

custom background uitableview with more than 1 section (IPHONE)

I need to set an image to my UITableViewController; if i have just one section on the tableview it works fine with:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"someImage.jpeg"]];
but if there are 2 section the image comes duplicate for each section.
Don't use a pattern image. Use the tableView's backgroundView property:
UIImageView * background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"someImage.jpeg"]];
[[self tableView] setBackgroundView:background];
[background release];