UITableView background image for all cells - ios

I want to change my tableview background image. When i change it, the change doesn't effect the cells. I'm using xib for my cells which doesn't have any code inside it, just design. So how can i solve this?
self.list.backgroundView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"Pattern.png"]];

In your cellForRowAtIndexPath method, set your cell's background color to clear color. It will make your cells transparent and show the background image.
Objective C:
cell.backgroundColor = [UIColor clearColor];
Swift:
cell.backgroundColor = UIColor.clearColor()

Related

UITableViewCell selectedBackgroundView does not work

My UITableViewCells are all pre-defined "Subtitle" style. I want to set the background image for a selected cell to another picture. I tried every which way to implement this and all methods discussed on stackoverflow seem to fail.
I tried again for some other, easier way to change the selectedBackgroundView property, like:
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds] ;
cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor];
But it doesn't work as well. What's wrong with that ?
As I understand you want to set selected background image to your cell?
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"backgroundImage.png"]];
EDIT:
As i know UITableViewCell can not be highlighted after selection in such common cases:
There is somewhere set cell.selectionStyle = UITableViewCellSelectionStyleNone;
You implemented willSelectRowAtIndexPath and it returns nil;
There is set [self.tableView setAllowsSelection:NO];
May be you set self.tableView.userInteractionEnabled = NO; or cell.userInteractionEnabled = NO;
You subclassed UITableViewCell and implemented such methods not correct setSelected:animated: or setHighlighted:animated
May be share your cellForRowAtIndexPath method code to investigate the problem
You can change the highlight color in several ways.
Change the selectionStyle property of your cell. If you change it to UITableViewCellSelectionStyleGray, it will be gray.
you can also check that property in your tableView:didSelectRowAtIndexPath:
//do something like this for color
cell.selectionStyle= UITableViewCellSelectionStyleGray;
// for image
cell.selectedBackgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"imageName"]];
Change the selectedBackgroundView property. Actually what creates the blue gradient is a view. You can create a view and draw what ever you like, and use the view as the background of your table view cells.
best of luck..
In my case the problem appeared after compiling on Xcode 13 - sdk iOS 15+
with any change to the code. Eventually I subclassed the cell and add the code:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
[self.contentView insertSubview:self.selectedBackgroundView
atIndex:0];
self.selectedBackgroundView.hidden = !selected;
}

UITableVIew controller in completely black color

I have requirement to show UITableViewController in completely black color.
I am facing below problem:
I have set background color of cell as black color. I have only four entries so those four cells are in black color with White Colored text. But now other empty cells are also displayed and those are in white colors.
Give Your
self.view.backgroundColor = [UIColor blackColor];
And also give
self.myTableView.backgroundColor = [UIColor clearColor];
If you does not want to see separatorStyle of your UITableViewCell then also write following code.
self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
If you use All Of this code then you can see your UITableView Completely black.
Make the tableView background color as black and make the cell's background Color as clear color. It should work.
just set background color black color of your tableview from xib like
And make your cell background color clearColor.
[self.tableView setBackgroundColor:[UIColor blackColor]];
Remove extra empty cells from the tableview.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if ([self numberOfSectionsInTableView:tableView] == (section+1))
{
return [UIView new];
}
return nil;
}
You need to tableview background color black color.
[yourtableview setBackgroundColor:[UIColor blackColor]];
and remove cell background color means Clear color , it will work fine for You.

table background clear - objective c - cocoa

Hi I want to display my table view without any background color so I used the following code to clear it
[uiTableView setBackgroundColor:[UIColor clearColor]];
This worked fine in the new iPad and also other generation iPads except it failed to work in iPad version one. The table shows with the background gray color. Is this something to do with iOS 5 ? Can anyone let me know if we can clear the background color in first version iPads.
I have noticed the same thing under iOS 5+. Try this:
[uiTableView setBackgroundColor:[UIColor clearColor]];
[uiTableView setBackgroundView:nil];
Should work as intended.
EDIT: If you're using the default cells, they cover the UITableView, so changing the UITableView's background color and view doesn't do anything. Try this in your tableView:cellForRowAtIndexPath: method:
cellName.backgroundColor = [UIColor clearColor];
cellName.backgroundView = nil;
As tableview has one more view as its background. You can set the backgroundColor in two ways.
i) Set the background color for the tableview and set the backgroundView to nil.
[uiTableView setBackgroundColor:[UIColor clearColor]];
[uiTableView setBackgroundView:nil];
ii) Set the backgroundColor for the tableView backgroundView.
UIColor *backgroundColor = [UIColor clearColor];
uiTableView.backgroundView = [[UIView alloc] initWithFrame:self.tableView.bounds];
uiTableView.backgroundView.backgroundColor = backgroundColor;

Change background of a grouped UITableView

I'm having some trouble trying to change the background of a UITableView with groups.
_tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"tableViewBg.png"]];
This usually works on every other UITableView, but not the one with groups, is there something else I have to do?
In IB I have the background color set to a clear color, but that doesn't do much.
You additionally need to disable the background view of _tableView:
[_tableView setBackgroundView:nil];
_tableView.backgroundColor = [UIColor redColor];
No need to add new view to backgroundView. This is working for me in iOS6.
why don't you set the tableView.backgroundView? you can alloc an image view withe the specified image and pass it to the background view instead of setting the background color.
What I usually do with grouped UITableViews is set the background color to clear, and the set that pattern image to the parents view.
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"tableBG.png"]];
self.tableView.backgroundColor = [UIColor clearColor];
tableView.backgroundView = nil;
tableView.backgroundColor = [UIColor colorWithPatternImage: xxx];
// tableView.backgroundColor = [UIColor redColor]; // is ok
if you set set the backgroundColor as this, when you scroll the tableView, the backgroundColor view will scroll also. so, you can: tableView.backgroundView = nil; self.view.backgroundColor = ...
The selected answer works but it clears the background of both tableHeaderView and table section header view. If you just want table header to be of a certain color say white and section header still to be default grey than do the following -
tableView.tableHeaderView.inputView.backgroundColor = [UIColor whiteColor];
Just want to add to Nirav's answer - it can also be done using the iOS 5 appearance proxy.
[[UITableView appearance] setBackgroundView:nil];
[[UITableView appearance] setBackgroundColor:[UIColor lightGreyColor]];
The advantage is that it is applies globally, so you can group all your UI customisations in one place. However, it will apply to all tableViews (not just grouped style).
For iOS 9+, changing the background color of the UITableView is enough.
Objective-C
tableView.backgroundColor = [UIColor redColor];
Swift
tableView.backgroundColor = .red
Swift 4.2
TO REMOVE BACKGROUND VIEW & COLOR
tableView.backgroundView = nil
tableView.backgroundColor = .clear

Transparent background for the Group Table Cell

For group table cell, I fall into this problem.
cell.backgroundColor=[UIColor clearColor]
make the cell bg black. It works for normal cell, not for group table cell.
I want to add some button, e.g. like the detail view of iPhone contact with transparent background.
If anybody face the problem, I got a solution, set a transparent view as a background view of the cell. Then it becomes totally transparent. Then you can add more view or customize the cell.
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
messageCell.backgroundView = backView;
messageCell.contentView.layer.cornerRadius = 10.0;
messageCell.contentView.layer.borderWidth = 1.0f;
messageCell.contentView.layer.borderColor = [[Settings getInstance] colorFrameBorder].CGColor;
messageCell.selectionStyle = UITableViewCellSelectionStyleNone;
return messageCell;
This solution was quoted in one of the StackOverflow question, which I cant remember.
I have also found that, its easy to add a transparent view in the table header or footer. The button down the contact details are probably added in a footer view.
From the looks of it I'd say you are setting a background image to your cell. You can see it at each cell on the right side, there are the stripes from your view background.
Remove the cell's background and you should be fine.
I found the solution from this answer here by setting cell's backgroundView
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
I would take Charles's answer one step further and do the following
self.myTableView.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];

Resources