how to set tableViewCell background to black gradient like navigationbar - iphone

I have set up my own custom tableViewCell and want to the background to be the same
black gradient as my navigationBar with style UIBarStyleBlackOpaque.
what would be the best way to do that ?

Use the key combination CMD-SHIFT-4 to capture a 1 pixel wide image of the navigation bar's gradient. The image will be saved on your desktop (you can use Preview to crop, etc.). Add the image to your project. In -tableView:cellForRowAtIndexPath:, use:
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"blackGradient.png"]];
UIKit automatically stretches the gradient horizontally to fit the width of the cell.

Related

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.

Obj-C, how can I add a background colour to image I'm adding to cell.imageview, its a PNG?

I am using PNG images to add icons to the rows in my tableview, using
[cell.imageView setImage:[UIImage imageNamed:#"chart.png"]];
This was not a problem before, as my images had black edges and my tableview had black rows. However, I am now providing a light color style as well as dark. So the black edges look rough.
So I thought I would draw the images with a black background.
However, I don't know how to do this?
UIImageView inherits from UIView so you can just set the backgroundColor property to show a color behind the transparant parts of your png image
[cell.imageView setBackgroundColor:[UIColor blackColor]];

UIImageView not showing the background View if the image has transparent regions

I have a UIView has some labels and buttons on it.
Next I also have a image which has a square area that is transparent, now
if I create a UIImageView and add this image which has transparent regions I am not able to see the background view (which has buttons and labels) through this transparent image.
If I play with the alpha value that doesn't work as intended which is to see the transparent regions exactly as it would have appeared on the UIView which has the labels and buttons.
UIImage* image = [UIImage imageNamed:#"TI1.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Also I would be interested to know if there is other way to achieve what I am trying to achieve.
Basically I want to highlight a certain area of the view which has buttons/labels and make the rest of the area greyed out. My idea was to have this UIImageView with transparent regions in image to achieve that.
Thanks
Ankur
Try setting imageView.opaque = NO;
UIImageView inherits from UIView. According to that class's docs:
This property provides a hint to the drawing system as to how it
should treat the view. If set to YES, the drawing system treats the
view as fully opaque, which allows the drawing system to optimize some
drawing operations and improve performance. If set to NO, the drawing
system composites the view normally with other content. The default
value of this property is YES.
Also, not sure that JPG even supports transparency, so try exporting the image as a PNG to ensure you get the results you're looking for.

Is there a way to fix the background image of a UITableView

I have set the background of my UITable with a custom image.
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
The problem I'm having is the background image is scrolling and doesn't look good at the top and bottom of the screen where you see the image being joined.
Is there a way to fix the position of the background image and just have the table scroll over the top of it?
Insert a UIImageView behind the UITableView. Set the UIImageView's image to background.png. Use [UIColor clearColor] as the table's background.
You need to put a UIImageView beneath your table view, set the table view's background color to clear (or in IB, set the opacity property of its background at 0). Then construct your CELL views to have clear areas that your background image can be seen through. You can do that programmatically or in IB (it's easier to visualize in IB, but the code to hook it all together is a bit tricky).

Removing image background in UIImageView at runtime

I have a ball assigned to a UIImageView in Interface Builder. An IBOutlet from the UIImageView is wired to a corresponding UIViewController. The image has a white background. When I assign it to the UIImageView in IB, the background is transparent. In IB, I have the UIImageView set to a transparent background and aspect fill.
When I assign the UIImageView an image at runtime:
self.ball.image = ballImage; //ballImage is a UIImage
self.ball.backgroundColor = [UIColor clearColor];
self.ball.contentMode = UIViewContentModeScaleAspectFill;
the UIImageView square has a white background where the ball doesn't display. Meaning all four corners. What is the difference that the IB version doesn't show a white background at runtime and the programmatic version does?
Make sure you set self.ball.opaque = NO; in addition to setting the background color to clear. Otherwise, a white background will still be drawn. I believe you have to set both of these whether you use IB or Xcode to create the view - but IB may have set them both for you.
If you need to remove background programmatically you may refer to "Bitmap Images and Image Masks" guide. I have no other idea on this point...just using CGImageCreateWithMaskingColors.
In case your *.PNG (or any graphic resource file) doesn't have built-in transparency (just white background) but with IB it looks like transparent - may be alpha property less then 1 for your UIImageView and you see partially transparent image.