How to put gradient style table cell in a table? - iphone

Hey, I am making a custom table cell right now but my custom cell is just white cell with some buttons and labels in background color which looks not that great.
Is there a way to render the background color so that the cell has some vertical gradient effect?
For example, on top of the cell it looks white and as it gets closer to center of the cell, it gets darker.
But as it gets closer to the bottom of the cell, it gets whiter again.
A great example would be this free app called "friendsaroundme".
Another thing is that I don't want to use custom image to do it (i.e make the cell.backgroundcolor = [uicolor colorwithpatternimage:somethingsomething.png..... ) because it's not that flexible.
So is there anyway to render the gradient style programatically?

You can use a CAGradientLayer as the background layer. Remember a table view cell is just a UIView. You can add a new layer to the layer tree with:
[[[self view] layer] addSublayer:gradientLayer];
I wrote a blog post on how to do this with UIButtons. The same technique applies.

Related

Different colors for grouped tableView and tableViewCell backgrounds

How can I set different colors for GROUPED UITableView inside background vs. background, which is outside my table? That is the part which is visible when tableView is smaller than the screen or scrolled beyond limits of table.
UPDATE: that sample image is generated by my current code! I DO NOT NEED TIPS HOW TO MAKE IT !!! Please read the question before you (try to) answer. Would really appreciate this, but thanx anyway.
File AboutViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.backgroundView = [[CustomView alloc]
initWithFrame:self.tableView.bounds]; // VISIBLE
self.tableView.backgroundColor = [UIColor redColor]; // NOT VISIBLE
}
In the sample image (generated by my current code) the background view contains sample gradient from yellow to green, each cell "bubble" background is texture, each label has own background - all this just to demonstrate what I could define. As you can see, background gradient is partially visible through each cell and remains static when you scroll the table.
What I want to do: I want DIFFERENT tableView background for GROUPED UITableView.
For plain UITableView style I have defined a custom background for each cell, but it doesn't seem to work with GROUPED tableView. This only sets the cell background inside the cell "bubble". How to define the cell area outside that cell "bubble"?
For this you can use the image as backgroundimage.
But I think you are using the grouped table,so
you need to go with the tableview background color
and set that to image color.
Like
winTableView.backgroundColor=[UIColor colorWithPatternImage:#"bg.png"];
Or you can even go with the Gradient layer,inserting that in you view.
Hope this will help you.
Get the image of different color as you want to show the background of your table view.
For this u set cell.backgroundColor =[UIColor redColor]; its work
and for background u tale image or set color
For cell color or view you can also use these.
Try to have different color for different cells by your logic.
cell.backgroundColor=[UIColor clearColor];
//cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.accessoryType=UITableViewCellAccessoryNone;
UIView *backroundSelecView=[[[UIView alloc]init]autorelease];
backroundSelecView.backgroundColor=[UIColor colorWithRed:203.0/255.0f green:218.0/255.0f blue:140.0/255.0f alpha:.80];
cell.selectedBackgroundView=backroundSelecView;
cell.backgroundView=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"table-grid#2x.png"]]autorelease];

UITableViewCell - How to set selection clipping?

In my app, I have a UITableView and each UITableViewCell utilizes a custom background and style with the following code in the "WillDisplayCell" method:
UIImage *cellBackgroundImage = [UIImage imageNamed:#"TableView_Cell_Background_iPhone"];
UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:cellBackgroundImage highlightedImage:cellBackgroundImage];
[cell setBackgroundView:backgroundImage];
[backgroundImage release];
The problem is that the PNG I'm using as the background is a rectangle, but the UITableViewCell is a rounded rectangle with a specific layer radius property. When I select a cell, the background overrides the rounded nature of the cell and I get a jarring blue highlighted sharp rectangle. Is there a way to set the selected state corner radius or something along those lines? My only other option if not would be to create a PNG background that fits the rounded rectangle perfectly.
Thanks.
I would set the selectionStyle of the cell to UITableViewCellSelectionStyleNone, this stops the blue highlight. Then start to do a custom selection job.
You will have to make a custom subclass of UITalbeViewCell. Override setSelected or if that doesn't work user a gesture recognizer or override touchesBegan etc. I have done it once, before, I forget exactly how, if you have trouble let me know and ill look it up.
When you detect that the cell is selected, perhaps create a translucent overlay as a PNG and make it appear when the cell is selected. Alternatively lower the transparency of the background image or add a color mask.

iPhone: TableView separator

I know that iPhone SDK can set a color to the TableView separator like this:
myTableView.style = UITableViewStylePlain;
myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
myTableView.separatorColor = [UIColor blackColor];
Is there any way to set gradient color to separator ? Thanks...
You can make the separator style UITableViewCellSeparatorStyleNone and apply a one point wide line to the bottom of each cell, which is a gradient.
The only problem is that it might look odd on tableviews with fewer cells than fit on the screen when the table view has a plain style, but I don't know how to fix that.
You could draw your TableViewCells (see Loren Brichter's fast scrolling) and then add gradient drawing to your drawContentView: implementation. For details on gradient drawing, see the "Drawing with a gradient" section of the CGContext documentation.
A great side effect of this is that your tableview scrolling becomes crazy fast... but you have to draw your cells, which can get complex if your cells have very complicated view hierarchies (which you should avoid anyway).

UIImage inside table view cell

Inside my table view for each cell I have an image along with some text and other stuff. When I select a cell I want the image also to be covered with blue overlay. Right now all other stuff excluding image is getting selected and a blue overlay covers the cell.
How to achieve this?
You won't be able to have that blue overlay to cover your image. The blue gradient is the backgroundView of the cell, whereas your UIImage is either drawn inside the contentView or added to it as a subview.
A possible workaround would be to add a CALayer on top of the whole cell that would mimic the default blue background.
It seems like there should be a way to do this without modifying your image, but all I can find so far is to specify a new image when the cell is selected. UIImageView has a property for both image and highlightedImage, so it would seem you need to provide your own image to use when the cell is selected, so you would need to set cell.imageView.image and cell.imageView.highlightedImage.
Sam's workaround would be a good approach, also. Especially if you don't want to provide your own image

Black corners on UITableView Group Style

I have a problem where my UITableView (group style) has a black "tip" above it's rounded corner.
I'm setting up the background of the tableview like so:
[meetingTableView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[[UIImage alloc] initWithContentsOfFile:#"background.png"]]];
And my table view ends up looking like this:
black pointy edge on rounded corner http://papernapkin.org/pastebin/resource/images/imageEntryId/6487
Any ideas how I can get rid of those black points?
I have got the same problem.
When I set clear color by xib, I have the back corner
The solution is to set it by code !
(The same problem with interface builder exist for webviews)
Try this in your controller's viewDidLoad method:
meetingTableView.backgroundColor = [UIColor clearColor];
You'll get Black corners on UITableView Group Style if you set background color to clear color in XIB.
Instead try this code for removing Black corners on UITableView Group Style
tableViewObject.backgroundColor=[UIColor clearColor];
Just in case you weren't already aware, there's another neat technique you can use to make customized backgrounds for UITableViews:
Not quite as simple as setting the background as you're doing, but it gives you a lot more flexibility and can scale to any table size.
Maybe if you put yourTableViewOutlet.backgroundView=nil;
To avoid the black corners you have to make sure that the UITableViewCells are not opaque. It looks like you're using custom styles table cells and the default value for opaque is YES. Either go to Interface Builder and uncheck the opaque checkbox if the table cell was set up in a XIB file. Or use the setOpaque:NO setter to change value.
Because the table cell view still has a rectangular frame even with the rounded corners the cell view and not the actual table view is causing those black corners.
My guess is that it's related to something that you're doing in your custom table view cells. You might want to experiment with setting the cell background color to [UIColor clearColor].
I think you should set the background color of your table as clearColor and initialsie your view with the background image.
Then it will definitely not show the black corners. But also don't forget to set the background color of your cell as white color
The up-voted answer above (set the tableView's background to [UIColor clearColor]) may not work for you if you are like me and never use the UITableViewController, instead putting a UITableView inside a UIViewController.
In this case it's not the tableView that needs to have a clear background, but the view that holds the tableview.
This is not intuitive, but it works for me. In interface builder you can just set the parent view's background color to clear color, or you could do the same in code in viewDidLoad with:
self.view.backgroundColor = [UIColor clearColor];
I'm guessing the reason for the black corners is something about the internal graphics optimization, and setting the background clear fixes it.