How to set UITableView background to image? - iphone

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

Related

How to make cell transparent on selection

I have a custom UITableView with its clearColor background. I'm setting the cell's backgroundView to a transparent UIImageView and setting the background image view's backgroundColor to [UIColor clearColor]. I'm also setting the cell's backgroundColor to [UIColor clearColor].
I want to make cell.backgroundview as transparent on selection. How to do that?
you can explicitly set a selectedBackgroundView property of the cell with a semitransparent or transparent content (UIimageview) so that i will not become opaque on selection
http://www.youtube.com/watch?v=PwcBdCUZNWs
i think thats what u r asking for just make the selectedView transparent from the UI properties
In your Cell creation :
cell.selectedBackgroundView =[[UIImageView alloc] init];
((UIImageView *)cell.selectedBackgroundView).image = [UIImage imageNamed:#"your transparent UIImage"];
or:
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = v;

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 change the background of table view , quickdialog box

I am following the tutorial about a quick dialog box to create a form as below
Now, I would like to change the background of this form. Does anybody know how to achieve this, please advice me on this.
Thanks
Try this, if you want to show an image as a background:
tableView.backgroundColor = [UIColor colorWithPattern:#"your_image_here"];
or
tableView.backgroundColor = [UIColor colorWithRed:128 green:128 blue:128 alpha:1.0];
If this does not work, you can set the backgroundView property of the table view:
tableView.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"your_image_here"]]] autorelease];
Use the backgroundView property of the UITableView.
If you want to use an image, just create a UIImageView and set backgroundView to it.
UIImage *myImage = [UIImage imageNamed:#"foo.png"];
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:myImage];
You may also use the contentMode to adjust how the image fits in the view.
self.tableView.backgroundView.contentMode = UIViewContentModeScaleAspectFill;
You could try some of the properties that UITableView either inherits or declares:
#property(nonatomic, readwrite, retain) UIView *backgroundView
#property(nonatomic, copy) UIColor *backgroundColor
The first is probably what you are going to want. It is declared by UITableView. The second is inherited from UIView.
If you're setting a background colour try this...
tableView.backgroundView = nil;
tableView.backgroundColor = [UIColor redColor]; //replace with your colour
But if you want a pattern image don't use that method... Do this..
tableView.backgroundColor = [UIColor clearColor];
UIView *v = [[UIView alloc] init]; //needs memory Managment (arc example)
v.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"pattern.png"]];
tableView.backgroundView = v;
This is because if the UITableView is group styled the and you have a patterned background image as the tableviews backgroundColor then it's applied to the edges of the cells too but as they're separate views the pattern becomes incorrect.
Hope it helps.
Try to set the background color of the table view; it may require that you change the table view style from grouped to plain.

Set background image in table view when story boarding is also used

I am doing the following to set the background image:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:#"appback_new.png"]];
[self.tableView setBackgroundView:imageView];
in view did load method. Also, I made sure that the background color is set to clear color in story board.
But when the view loads, it loads with a black color.
Did any one face this issue and knows how to resolve it?
Thanks.
Try this it should work :
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"appback_new.png"]];
self.tableView.backgroundColor = background;
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"appback_new.png"]];
try your code after inserting this line of code
tableView.backgroundColor = [UIColor clearColor];

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