Right and left margin to UITableViewCell - iphone

I created the following UITableViewCells and set the background to white.
(the black frame is the simulator frame).
I would like to add margins right and left to the cell.
I looked in many Q&A and they all suggested customizing the cell with UiImageView. Is there another way to do it?

add UIView as contentView of the cell:
in cellForRowAtIndexPath
UIView *viewLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, heightOfTheCell)];
viewLeft.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:viewLeft];
UIView *viewRight = [[UIView alloc] initWithFrame:CGRectMake(310, 0, 10, heightOfTheCell)];
viewRight.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:viewRight];
and in this way you can have round corners:
[cell.layer setCornerRadius: 5];
[cell.layer setMasksToBounds:YES];
[cell.layer setBorderWidth:2.0f]
CGColorRef colorRed = [[UIColor redColor] CGColor];
[cell.layer setBorderColor:colorRed];

Related

Can't get rid of color in UITableView's tableFooterView

I have a standard UITableView with three sections. At the bottom of the table I'm trying to add a tableFooterView. It seems as though there's a standard spacing between the last row in the table and the top of the tableFooterView, which is fine, but my problem is that there is a different color in this spacing that I can't get rid of. I need that spacing to match the background of my tableView.
Here's some code:
self.menuTable.backgroundColor = [UIColor redColor];
self.menuTable.backgroundView = nil;
self.menuTable.separatorColor = [UIColor clearColor];
self.menuTable.delegate = self;
self.menuTable.sectionHeaderHeight = 10.0;
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
footer.backgroundColor = self.menuTable.backgroundColor;
UILabel *footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 205, 100)];
footerLabel.text = #"foo bar.";
footerLabel.textColor = [UIColor blackColor];
footerLabel.backgroundColor = self.menuTable.backgroundColor;
footerLabel.numberOfLines = 10;
footerLabel.font = [UIFont italicSystemFontOfSize:11.0];
[footerLabel sizeToFit];
[footer addSubview:footerLabel];
self.menuTable.tableFooterView = footer;
Here's a screenshot (sorry about the red, but it's the easiest way to see the differences in colors).
Whoops. Earlier I was trying to add a sectionFooterView, and had implemented tableView:heightForFooterInSection:, returning 20 for the last section. That's where that gap was coming from, and it had a default color of white. Since I'm now placing the text as the tableFooterView, I just removed that delegate callback and everything is rendering as expected.
Try this
self.menuTable.backgroundColor = [UIColor clearColor];
self.view.backgroundColor = [UIColor redColor];

Translucent UITableViewCell has no borders

I created a UITableView. I gave it a background image like this:
UIImageView *imageview1 = [[UIImageView alloc] init];
imageview1.image = [UIImage imageNamed:#"blood2.gif"];
evc.tableView.backgroundView = imageview1;
And then I made the cells translucent by changing them in the tableView:cellForRowAtIndexPath: method:
UIView *backgrView = [[UIView alloc] initWithFrame:CGRectZero];
backgrView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
cell.backgroundView = backgrView;
But now there are no borders around the UITableViewCells anymore. I already set the separatorColor to black but the borders won't show up...
Check with this
[cell.contentView.layer setBorderColor:[UIColor redColor].CGColor];
[cell.contentView.layer setBorderWidth:2.0f];

UITableView cell takes pattern color of UITableView's background image

I've added custom background image to my UITableView and it shows fine. Than, i removed any background color from the cells. Now i expect to see only cells' text on top of table's background image, however i see strange behaviour. Cells take pattern color of table's background image, so that i see waves like in the picture:
The background image shell look like this:
*pay attention to white circles that disappear under the cells!
My code is as follws:
in viewDidLoad i set the background image of table view:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"general_bg.png"]];
For each cell i remove any background color like this:
UIView *bckView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
bckView.backgroundColor = [UIColor clearColor];
cell.backgroundView = bckView;
cell.backgoundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(x,y,x1,y1)];
textLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:textLabel];
line separators are custom images added to cells.
Instead of setting the BackgroundColor attribute of the tableview, try setting the backgroundview:
UIView *background = [[UIView alloc] initWithFrame:self.tableView.bounds];
background.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"YOUR_IMAGE.png"]];
self.tableView.backgroundView = background;
This should fix the issue of the cell's taking on the same image as the background.
self.tableView.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"YOUR_IMAGE.png"]];
maybe this is what you're looking for

Create alpha UIView with an UIlabel in front of it

i would like make an UIView, with an alpha and with a label.
but i want the UILabel in front of all like this:
How to do it?
Here the code:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 20)];
lbl.text = #"AAAAAA";
lbl.backgroundColor = [UIColor clearColor];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(50, 210, 230, 50)];
v.backgroundColor = [UIColor greenColor];
v.alpha = 0.5;
[v addSubview:lbl];
[self.view addSubview:v];
}
The green view is with alpha 0.5... like the text and this is wrong!!!
thanks.
Instead of setting the alpha of the whole view, just set the background color to a color with transparency.
So change this:
v.backgroundColor = [UIColor greenColor];
v.alpha = 0.5;
To this:
v.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.5];
If you want to use the built in colors, see colorWithAlphaComponent:
accessoryView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
In the XCode 4 Interface Builder you can achieve this by clicking on the Background property of a view, clicking on "Other", and choosing the color and the opacity values in the color picker that appears.

How to make semi-transparent UITableViewCells?

I hope to add image to cell background and from the semi transparent cell I could see the backgroud of tableview.
From following topic, I know how to make transparent cell.
UITableViewCell transparent background (including imageView/accessoryView)
I've tried above, it did work. But when I add the following code:
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor colorWithRed:.1 green:.1 blue:.1 alpha: .4];
cell.backgroundView = backView;
The cell became grey color without any transparency, if I change code as follows,
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
The cell became white, still without any transparency.
Is there one can give me any clues?
To make your UITableViewCells Semi-Transparent that is Translucent, you have to make your UITableViewCells Transparent and then, set cell's backgroundView to an image which has translucent properties. Also set selectedBackgroundView of the cell to an image which is translucent and gives a selected cell's effect.
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.text.backroundColor = [UIColor clearColor]; //if you have labels on cells..
cell.backgroundView = backView;
Then set an image with translucent effect on our transparent cell..
UIImageView *cellImage = [[UIImageView alloc] init];
[cellImage setImage:[UIImage imageNamed: #"unselectedCellImage.png"]];
[cell setBackgroundView:cellImage];
[cellImage release];
Then set an image for selected cell to give an translucent effect after selection also..
UIImageView *cellImageSelection = [[UIImageView alloc] init];
[cellImageSelection setImage:[UIImage imageNamed: #"selectedCellImage.png"]];
[cell setSelectedBackgroundView:cellImageSelection];
[cellImageSelection release];
It'll work..
Cheers..
You can try this one
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor colorWithRed:255 green:255 blue:255 alpha: .2];
cell.backgroundView = backView;
What you did wrong was you choose .1 .1 .1 which is for black color you need 255 255 255 for the white color and your opacity was very high so it looked gray color to you actually it was black with opacity. Hope this helps :)
Views are initialized as opaque by default, so you probably need to explicitly set backView.opaque = NO; in order for it to draw with any transparency.