Unable to set transparency in custom cell created programmatically - iphone

I have a UITableViewController with custom cells, based in Tweetie's Fast scrolling example and i need transparency.
Until now, i loaded my cells from a nib and all i needed was to set some table's properties to
table.backgroundColor = [UIColor clearColor];
table.opaque = NO;
table.rowHeight = 130.0f;
table.separatorStyle = UITableViewCellSeparatorStyleNone;
in order to make the table transparent. As for the cell, i did:
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cellbackground.png"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cellbackground.png"]];
And so i had every cell with a background image but with the rest completely transparent (half of the image is completely transparent). Now, creating the cell programmatically and drawing everything myself, i just can't get to make the cell transparent. The image part looks fine, but everything else is black, not opaque

Figured it out.
The issue was that it wasn't paying attention to my
self.opaque = NO;
because my cell's super class was doing exactly the opposite, setting opaque = YES;. So i changed that and it is working great now.
PS: Thanks for answering and making me pay extra attention to opaque property.

Try this:
cell.backgroundColor =[UIColor clearColor];
You may also need this:
cell.opaque = NO;

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

iPhone: Grouped TableView background image problem

Hey everyone, this seems like it should be a simple one; I really hope it is. As always, thanks in advance!
All I'm trying to do is add a background image to a grouped style tableview. I'm using the following method in viewDidLoad:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
Unfortunately, background.png seems to be drawn not only behind the cells, but also on the backgrounds of the cells and the headers as follows:
alt text http://www.freeimagehosting.net/uploads/194449ffc1.png
I know there are a few questions on this site addressing similar issues, but I'm afraid I couldn't get anything to work. How can I set the background of UITableView (the tableview style is "Grouped") to use an image? led me to add
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;
to viewDidLoad and to add
cell.backgroundView = nil;
cell.opaque = NO;
in configuring my cells. Needless to say, it didn't work. Adding
cell.backgroundColor = [UIColor clearColor];
didn't work either. And sadly, that was my last idea. I'd really like to be able to do this without adding the background in interface builder, if possible. I rather like doing as much as I can programmatically. I hope asking that isn't too greedy. Thanks again!
*
*
*
EDIT:
*
*
*
I came across another approach to accomplishing this, but ran into another problem. I added the following code to viewDidLoad:
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
[self.tableView.window addSubview:bgView];
[self.tableView addSubview:bgView];
[self.tableView sendSubviewToBack:bgView];
It looked promising, and it likely is, but unfortunately in gave me the following:
alt text http://www.freeimagehosting.net/uploads/c02dd68e20.png
It looks as though it either wasn't sent to the back and the headers somehow show through or the cells went to the back with it. I'm not at all sure. Any advice here would be appreciated as well.
Here's where I found the code for the edit:
Add UIView behind UITableView in UITableViewController code
I went through a similar path to what you describe here. I finally found an answer that worked for me on the following post:
How to set the background of a UITableView to an image?
It was two lines of code posted by user rkb. Here's the code:
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];
Another solution if nothing else here is working out. In ViewDidLoad:
UIImage *background = [UIImage imageNamed:#"MyBackground.png"];
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:background];
This will place your static (doesn't stretch or move) background behind all of your grouped tableview elements, such as the headers and cells.
Using the previous responses(particularly from dana_a) this is how I got the same thing to work
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundcolor = [UIColor colorWithPatternImage:[UIImge imageNamed:#"bgtest2.png"]];
This is in my viewWillAppear: method in for my case a detail view controller with a grouped tableview.
I placed this code in my ViewDidLoad method and it worked beautifully.
self.termsTableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"metal4.png"]];
I still haven't figured out why the above doesn't work or how to modify it so that it does, but by adding the subView to the window in the App Delegate instead of the tableView here I was able to get it to work.
Instead of just getting it to function and moving on I'd like to actually learn, so I'm still curious as to where I went wrong above. As such, I'll leave this as unanswered for a time and if you can tell me my error(s) I'll give you the up vote and the green check mark. Thanks!

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.

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

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.