Background Image UITableView changes when scroll [duplicate] - ios

This question already has answers here:
iPhone Fixed-Position UITableView Background
(5 answers)
Closed 9 years ago.
I set the backgroundImage of my tableView:
self.tableView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"Content.png"]];
And I set transparent the backGroundColor cells of my table View:
SBQCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil){
cell.backgroundColor=[UIColor clearColor];
}
When I do scroll, the backGroundColor of the Table View changes.
Capture 1 (Without Scroll):
Capture 2 (With Scroll):
Thats the image Im using as background:
What can I do to skretch the Image background to the full table View? Thanks

Make the background on a view layer behind the TableView, not on the TableView itself and then set a clear background on the UITableView
or...
Set the tableview.backgroundView not the backgroundColor

Solved with:
self.tableView.backgroundView=[[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"ContentBg.png"]];
More:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.backgroundColor = [UIColor clearColor];
}
The cells keep transparent when I do scroll and the image for the backGround of the Table View is resized.
Thanks everyone!

You're adding the background image as a pattern to the backgroundColor property.
Maybe try this:
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Content.png"]];
[self.tableView setBackgroundView:iv];
See the Apple UITableView class reference here:
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableView/backgroundView

You have:
if (cell==nil){
cell.backgroundColor=[UIColor clearColor];
}
I think you mean:
if (cell!=nil) {
cell.backgroundColor=[UIColor clearColor];
}

Related

How to set background color only for half of the tableView cell

I want to only color my left half or right half of my tableview cell background. How can I manage that? For now, I am only able to set the background color for the entire row. I have tried to put a label on the left half of the cell, and set the background color of the label. But the problem is then the label covered the cell.text so that I cannot see the content of the cell. I really want to know if I can set the label just under the cell text but above the cell's background. so it can does its work. But other approach to accomplish just coloring half of the tableview cell is appreciated as well!
I have tried #winnder.ktw to set it up programmatically. however I got an error image how can i fix that??
thank you!
If you're using interface builder, just change the order of the views in the Document Outline panel at the left, and you can choose what's on top of what:
If all you need it the background there's no need to add a label, just add a regular view to your cell.
Edit: here, just add a view and a label, and make sure the label is below the view in the Document Outline panel.
try this
- (UITableViewCell *) tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier: #"any-cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: #"any-cell"];
UIView* bgView = [[UIView alloc] initWithFrame: cell.bounds];
bgView.backgroundColor = [UIColor whiteColor];
UIView* halfView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, cell.bounds.size.width / 2.0, cell.bounds.size.height)];
halfView.backgroundColor = [UIColor greenColor];
[bgView addSubview: halfView];
cell.backgroundView = bgView;
}
return cell;
}

Background color custom uicollectionview

I have a UICollectionView which contains custom UICollectionViewCell. The collection view has a background image.
I would show the background image, but the collection view (or its cells) has always a white background which prevents my background image to show.
Here is the code:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
AgendaCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:agendaCellIdentifier forIndexPath:indexPath];
cell.delegate = self;
cell.layer.borderWidth=0.0f;
cell.layer.backgroundColor = [UIColor clearColor].CGColor;
cell.contentView.layer.backgroundColor = [UIColor blackColor].CGColor;
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
[cell setCurrentDateAtIndex:([indexPath row]) date:currentMonth events:events];
return cell;
}
The AgendaCollectionCell is very simple: I did a cell by using IB and set all background images to clearColor. Also the collection view use a very basic custom layout.
An excertp of what is shown in the pic:
The collection view show cells with numbers. Beneath the collection view there is a background which is not shown because of the white color of the cells.
Any hint appreciated. Thank you.
SOLVED.
The problem was in the rendering of the custom cell. I used a method (a lost method...) which set the background color of the cell. For non custom cell, the code in my question is correct.
you can also background color for your custom UICollectionView by setting backgroundview for UICollectionView.
UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
self.collectionView.backgroundView = blueView;

UITableViewCell's backgroundView and corner radius issue in grouped tables

I've a grouped UITableView and I've set the background of one of its cells to be a gradient image, this way:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
cell.textLabel.text = NSLocalizedString(#"Add item", #"");
cell.textLabel.backgroundColor = [UIColor clearColor];
UIImage* image1 =[[UIImage imageNamed:#"gradient_normal"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
UIImage* image2 =[[UIImage imageNamed:#"gradient_selected"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
cell.backgroundView = [[UIImageView alloc] initWithImage:image1];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:image2];
}
return cell;
}
Background of the cell is correctly displayed, but it has not rounded corners anymore, as the rest of the cells at top and bottom of the table and with default background have... I tried setting the cornerRadius property of the table view, but it didn't work. And I was not able to set the cornerRadius for the particular cell.
The only post I found dealing with this same problem had not been actually answered (grouped tableViewCell - no rounded corner after setting the background of a grouped table cell), could somebody help me with this issue?
Thanks in advance
If you are allowed/able to use outside libraries, checkout PrettyKit, makes it quite easy.
If you have to do it on your own, the best way to go about this is to subclass UITableViewCell and create your own custom cells. Here is a good tutorial

IOS 5 static tableview cell no background color

I am trying to make a table cell completely transparent. I am using static table cell in iOS 5 and tried change to clear color, but it does
I even tried (in viewDidLoad)
self.tableView.backgroundColor = [UIColor clearColor];
But the whole table is black and the cell is white. (See photo)
Try this
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor clearColor];
}
self.tableView.backgroundColor = [UIColor clearColor] - will change the background color of the tableview not the cells.
Try selecting the individual cells in Interface Builder and using the inspector to change the cell background to a color or nothing for transparent.
Try [cell setOpaque:FALSE] and [cell setBackgroundColor:[UIColor clearColor]] in cellForRowAtIndexPath:

BackgroundColor of UITableViewCellStyleSubtitle labels?

I am trying to create a table with an image as a background.
To achieve this, I started out with the background:
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
This resulted in a background image which appears in tablecells as well. That is not something I want, so I tried to set the cell's backgroundColor:
cell.backgroundColor = [UIColor whiteColor];
This had no effect at all !!!. Hmmm strange.
So I tried this:
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor whiteColor];
cell.backgroundView = backgroundView;
[backgroundView release];
This works almost. The only problem left is that the textLabel & the detailTextLabel still show the background behind them.
Setting the backgroundColor on those labels to white doesn't do anything either.
How do I proceed? Where do I go wrong? I am trying to achieve a tableView like the worldClock app has.
To change the background colour of the table view cell, you'll need to set it in tableView:willDisplayCell:forRowAtIndexPath: rather than tableView:cellForRowAtIndexPath:
otherwise it won't have any effect, for example:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor whiteColor];
}

Resources