UITableView Cells background color NOT changing? - ios

I have a UITableView set up in two storyboards - one for iPhone and one for iPad.
I have set it up in the iPhone storyboard such that the cells have a clear background and so a background image shows through - this works perfectly fine.
HOWEVER, when I copy this view in to the iPad storyboard, for some reason the table cells have a white background even though all the settings are the same (clear colour still set in the builder).
What could be the reason for this?
EDIT - I've looked at this further and if I change the colour of a row to anything, it won't change - for some reason it remains white :/

Appears to be a known issue with this, and the fix is by adding the following to the .m file for your class:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor clearColor]];
}

You have to set the backgroundColor of the cell's contentView to your color
cell.contentView.backgroundColor = [UIColor yourcolor];

From iOS 7, UITableViewCell have a default white background.
Set: cell.contentView.backgroundColor = [UIColor clearColor];
and eventually : cell.contentView.backgroundView = nil;
And verifiy also your:
tableview.backgroundColor = [UIColor clearColor];
tableview.backgroundView = nil;

extension UITableViewCell {
func setTransparent() {
let bgView: UIView = UIView()
bgView.backgroundColor = UIColor.clear
self.backgroundView = bgView
self.backgroundColor = UIColor.clear
self.layer.backgroundColor = UIColor.clear.cgColor
self.contentView.backgroundColor = UIColor.clear
}
}
and call that method in cellForRowAt

Related

background for uitableview cell

MyTableCell *cell=[[MyTableCell alloc]init];
cell.backgroundColor = [UIColor clearColor];
The above lines of code are still showing the white color of the cell's background and hiding the background image of the tableview.
Go to you storyboard and set the color of your uitableview cell to clear color from there.And also set uitabelview color also to clear color.
Or in you viewdidload set tableview background color to clear color and in cellforrow at indexparth delagate set
cell.contentView.backGroundColor = [UIColor ClearColor]
try this
- (void)tableView:(UITableView *)tableView willDisplayCell(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
set the contentview background color also
cell.contentView.backGroundColor = [UIColor ClearColor];
cell.backGroundView.backGroundColor = [UIColor ClearColor];
cell.selectedBackGroundView.backGroundColor = [UIColor ClearColor];
Also check if you cleared the tableView.backGroundColor also
Thank you every one. I have solved the problem. I changed the background attribute in the attribute inspector in the storyboard for my cell object from default to clear color and it worked.
You can do it this way
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView.backgroundColor = [UIColor clearColor];

Select row and change button color

In My tableview, cell is custom cell and I have button and label inside it.
lable and button background color is Green.
When I click on Particular row then Button color change and label background color is as it is. Here I used following code to set label background color via layer,
cell.lblCountMsg.backgroundColor = [UIColor clearColor];
cell.lblCountMsg.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0].CGColor;
but, same code is not working for Button. i tried with layer also
cell.btnTeting.backgroundColor = [UIColor clearColor];
cell.btnTeting.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1].CGColor;
but not set green color and also changed the color of selection color.
See, My Screen Shot (Breaking new) is Green.
Please help me to find solution.
Thanks in Advance.
You should implement didSelectRowAtIndexPath something like,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Here your custom cell's class instead of UITableViewCell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.btn.backgroundColor = [UIColor redColor];
}
If you made a UITableViewCell subclass, try overriding the
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
method. In this method you can change your views in the cell when the cell is highlighted.
So for example:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
self.btnTeting.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0];
}
UPDATED
#interface SuggestedGroupCell ()
#property (strong, nonatomic) IBOutlet UIButton *btnSubScribe;
#end
#implementation SuggestedGroupCell
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
self.btnSubScribe.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0];
}
#end
If you want to change background color of button,remove this line and then try:
cell.btnTeting.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1].CGColor;
But if you want to change button title color you can write code like this:
[cell.btnMorePupils setTitleColor:[UIColor redColor] forState:UIControlStateNormal]
Here UIControlState you can take what ever like UIControlStateNormal, UIControlStateSelected etc as per your requirement.

Footer view's color always darker than UITableView separator's color

In my UITableView, I set the separator color like this:
- (void)viewDidLoad {
...
self.tableView.separatorColor = [UIColor lightGrayColor];
...
}
And I set the color of the footer like this:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UITableViewHeaderFooterView *footerView = [[UITableViewHeaderFooterView alloc]
initWithFrame:(CGRect){0, 0, 320, 1}];
footerView.contentView.backgroundColor = [UIColor lightGrayColor];
return footerView;
}
However, the footer view's color is always darker than the separator's color, like this:
How do I get them to be the exact same color? Thanks.
From iOS 6.0 onwards, you can use the below mentioned UITableView's delegate method to change the color of the footer view's background-
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
//Set the background color of the View
view.tintColor = [UIColor blueColor];
}
None of the above worked for me. The only thing that worked was the solution from this thread:
White Separator Above Table Section Headers
Basically you need to manually add in the footer separator 1 px above your custom footer view.
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UITableViewHeaderFooterView *versionView = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, maxScreenWidth, 50)];
UIView *footerSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, -1, maxScreenWidth, 1)];
versionView.backgroundColor = [UIColor blackColor];
footerSeparator.backgroundColor = [UIColor darkGrayColor];//Your color
[versionView addSubview:footerSeparator];
return versionView;
}
The only problem I have with this is that the footer separator seems to disappear if you scroll away from it and then back to it in the table view.

cannot set background color of UITableViewCell in iOS6?

I started testing my app under simulator, because I don't have any iOS 6 device and stumbled upon weird problem. I cannot set backgroundColor property of UITableViewCell. If I st this:
cell.contentView.backgroundColor = [UIColor redColor];
it is working only for iOS 6, when I use this:
cell.backgroundColor = [UIColor redColor];
or this
[cell setBackgroundColor:[UIColor redColor]];
it is working only for iOS7.
When I use both cell.contentView and cell.backgroundColor it's working for both iOS... shouldn't it be one answer for such a 'easy' property? Or maybe it's some simulator bug?
UPDATE:
If it changes anything in the same tableview and cells I cannot set accessoryType neither via StoryBoard nor code...
UPDATE2: for some reason setting tableview style to plain deleted all my changes, but grouped showed as expected...
In iOS6 you need to change the cell's backgroundColor in willDisplayCell.
This works in iOS7 as well, precluding the need for version-specific code.
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor redColor]];
}
I don't see the problem in setting the background for the contentView AND directly to the cell. iOS 7 has change a lot this class. If you need to be compatible with old systems then you need to do this type of things.
So yes, you should use both:
cell.contentView.backgroundColor
cell.backgroundColor
Try to change the cell's backgroundView color not the backgroundColor.
UIView *myView = [[UIView alloc] init];
myView.backgroundColor = [UIColor whiteColor];
cell.backgroundView = myView;
If you set (assuming you use standard cell):
cell.textLabel.backgroundColor = [UIColor clearColor];
...then you just have to use:
cell.contentView.backgroundColor = [UIColor redColor];
iOS 6 seems to copy table's background color to textLabel.
try to add this code:
cell.backgroundView.backgroundColor = [UIColor clearColor];

UITableView extend different background color below cells

I have a UITableView that I have given a headerView that has a green background. I have also given the UITableView that same green background color so that when the tableView is pulled down it feels like a seemless color above the tableView. Works perfectly.
The problem is my tableView only has 4 rows, and below that it shows the green color again.
How can I show white below the rows instead so that I only see the green background color when pulling down the tableView?
You can add an extra top view, same as in this answer to UIRefreshControl Background Color:
CGRect frame = self.tableView.bounds;
frame.origin.y = -frame.size.height;
UIView *bgView = [[UIView alloc] initWithFrame:frame];
bgView.backgroundColor = [UIColor greenColor];
[self.tableView insertSubview:bgView atIndex:0];
You could add a fifth cell with no content, and make it, say 400 points high, and limit the size of the table's contentView so that you can't scroll to the bottom of that cell.
-(void)viewWillLayoutSubviews {
self.tableView.contentSize = CGSizeMake(self.tableView.frame.size.width, 500);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat h = (indexPath.row == 4)? 400:44;
return h;
}
Putting the contentSize setting in viewWillLayoutSubviews ensures that it will be reset to that value if you rotate your device.
as I said in these comments, in this way the headerView will be under the navigationBar and if you pull down the tableview you will see the headerView green. So you can set white color for tableView:
(i tested it and work)
-(void) viewDidLoad {
self.tableView.backgroundColor = [UIColor whiteColor];
headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor greenColor];
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, 320, 120)];
lab.backgroundColor = [UIColor greenColor];
[headerView addSubview:lab];
self.tableView.tableHeaderView = headerView;
}
#pragma mark - Table view data source
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 80;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return headerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
Same as the accepted answer - in Swift 5:
var frame : CGRect = self.tableView.bounds
frame.origin.y = -frame.size.height
let bgView : UIView = UIView.init(frame: frame)
bgView.backgroundColor = UIColor.green
self.tableView.insertSubview(bgView, at: 0)
Also add the following code (right after the above code) to make sure it works for other sized devices:
bgView.translatesAutoresizingMaskIntoConstraints = true
bgView.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth]

Resources