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