Transparent background in grouped UITableView - iPhone - iphone

I want to make the grouped UITableView transparent. I partially succeded with the following code:
UIColor *bgColor = [[UIColor alloc] initWithWhite:1 alpha:0.0];
historyTable.backgroundColor = bgColor;
Unfortunately, black corners appeared in the rounded cells. How to get rid of them?

Instead of using
UIColor *bgColor = [[UIColor alloc] initWithWhite:1 alpha:0.0];
historyTable.backgroundColor = bgColor;
Just use:
historyTable.backgroundColor = [UIColor clearColor];
That also clears up the memory leak you were creating.

remove UITableView backgroundView
xxx.backgroundView = nil;
This is necessary on iPad builds. When compiling to run on iPad and iPhone, check the tableView responds to the selector with ...
if ([self.tableView respondsToSelector:#selector(setBackgroundView:)]) {
[self.tableView setBackgroundView:nil];
}

for me it worked finaly after setting both to nil/clear:
[myTableView setBackgroundView:nil];
[myTableView setBackgroundColor:[UIColor clearColor]];

I had this issue and found that there was no difference between using:
[[UIColor alloc] initWithWhite:1 alpha:0.0];
and using:
[UIColor clearColor];
I tried both of these and still had the little black corners on my table view.
I also tried setting the backgroundView to nil as suggested, but this didn't work either.
I solved this by setting the backgrounds of the individual cells to transparent in the cellForRowAtIndexPath method:
cell.backgroundColor = [UIColor clearColor];
Of course, this has the side effect that your cells themselves are transparent, which isn't ideal for everyone, but it ok for me in this case.

Related

Problem with UITableView clear color

I have a tableview in grouped style. When I set the tableview background color to clear color then I see dark black corners around the tableview. Is there a way to solve this problem and remove those dark corners?
Any help would be appreciated.
Along with using clearColor, use the following:
[tableView setBackgroundView:nil];
[tableView setBackgroundColor:[UIColor clearColor]];
[tableView setOpaque:NO];
You can set a UIView and set clear color to it. This works for me.
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;

Removing cell borders from a section of grouped-style UITableView

I have a UITableViewController initialized with the grouped style and having multiple sections. For one of these sections, I'd like its constituent cells to be completely transparent and have no border. I plan to assign a custom view for every row in this section, but having that custom view surrounded by the grouped table cell looks bad :(
The following makes the background color of a cell black instead of transparent... And I still don't know how to get rid of the border.
cell.backgroundColor = [UIColor clearColor];
Any pointers? Thanks!
NOTE: This doesn't appear to be working in iOS7 and above. For iOS7 try this answer.
For iOS6 and below, to remove the grouped background from a cell in a grouped table view cell:
This didn't work
cell.backgroundView = nil; // Did Not Work
This did
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
If you have moved to ARC (I've heard this works, but haven't tested it)
cell.backgroundView = [UIView new];
You have to actually set
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
to remove the border of cells.
The following hack works in iOS 7 – for now. :)
Subclass UITableViewCell, and use this cell for the section that shouldn't have separators.
Override the addSubview method in your cell subclass:
-(void)addSubview:(UIView *)view
{
// The separator has a height of 0.5pt on a retina display and 1pt on non-retina.
// Prevent subviews with this height from being added.
if (CGRectGetHeight(view.frame)*[UIScreen mainScreen].scale == 1)
{
return;
}
[super addSubview:view];
}
This is what worked for with having a Grouped style table
[tableView setSeparatorColor:[UIColor clearColor]];
This code worked for me :)
[self.tableView setSeparatorColor:[UIColor clearColor]];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Set the backgroundView of the cell to nil. For a grouped table, the cell image is part of that view.
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
Try using tableView.separatorColor = [UIColor clearColor];
And, don't use tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
I tested with both, if style is none, making the section borders invisible is not working, but instead just change its color, and section border will appear to be none.
iOS seems to be differentiating making an object none and making an object transparent
cell.backgroundView = [UIView new];
Works like a charm! Tested! iOS6
As of iOS 8, setting the separator attribute to none works as well.
Setting a content view also gets rid of the border. Set your custom view to cell.contentView.
The easiest way to remove cell borders from a section of grouped-style UITableView:
[tableViewOutlet setBackgroundView:nil];
in the viewDidLoad method.
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
cell.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:imageView];
If you have a custom UITableCellView then you can add the following method to your view to remove the background view.
- (void)setBackgroundView:(UIView *)backgroundView
{
// We don't want background views for this cell.
[super setBackgroundView:nil];
}
I just thought I would convert my comment to #Intentss into an answer, because it maybe useful for those, using his solution.
Using iOS6.1 with a grouped UITabelView, using ARC:
[tableView setSeparatorColor:[UIColor clearColor]];
Does not work
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
Does work

Changing the color of UITableView section headers

OK, I know that this has been asked previously, so please forgive me for asking again. It just seems like there has got to be an easier way to do this.
Is there a 'simple' way to change the UITableView section header background color? I know that I can use the delegate method 'viewForHeaderInSection' to hand the UITableView a custom UIView to use for the section headers, but all I really want to do is set the 'tintColor'. There is a tintColor property on the search bar, why not for the section headers.
If I use viewForHeaderInSection to create my own custom UIView, I have to create my own label as well. I have to style and position my label to look just like the rest of the Apple standard. I have to style and size the 'background' to look like the rest of the Apple standard.
I can't find a way to simply ask for the section header and then change it's color. Am I missing something or do I really have to do this the hard way.
If I have to do this the hard way, does someone have all of the styling information that matches the Apple standard?
- bar is 'translucent'
- bar has some shading on the top and the bottom
- label has a certain size, position, shadings, etc
Thanks. And once again, sorry for asking this question again.
With the newer UIAppearance way of doing things, in versions > iOS 6.0 you can just do, for example:
[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];
With UIAppearance you can change UILabel text color on your UITableView headers like this:
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setShadowColor:[UIColor whiteColor]];
Should work on iOS 6.0+. Can be called anywhere (viewDidLoad etc).
This is something I spent quite a while grappling with myself, only to find that there is no way to just change the tintColor of the section header. The solution I came up with was to screenshot the background of the section header, change the tint of it in Photoshop, and then use that as the background of the section header. Then its just a case of laying out the label.
As you said, the thing to use is the viewForHeaderInSection delegate method.
Here is what I've found works and looks like the Apple default:
UIView *customView = [[[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 320.0, 22.0)] autorelease];
customView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"headerbackground.png"]];;
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
headerLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
headerLabel.frame = CGRectMake(11,-11, 320.0, 44.0);
headerLabel.textAlignment = UITextAlignmentLeft;
headerLabel.text = #"Header Label";
[customView addSubview:headerLabel];
return customView;
Here "headerbackground.png" is a 1 pixel by 22 pixel (double that for iPhone 4) image that will get repeated across the length of the header.
Hope this helps!
If you change the appearance, like in the other answers, it is application wide.
If you want to change just for a specific tableview, implement:
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
UITableViewHeaderFooterView * headerview = (UITableViewHeaderFooterView *)view;
headerview.contentView.backgroundColor = [UIColor greenColor];
headerview.textLabel.textColor = [UIColor whiteColor];
}
The UIAppearance method appearanceWhenContainedIn has been deprecated as of iOS 9.0; instead you can still do the exact same thing with the appearanceWhenContainedInInstancesOfClasses method and an array.
[UILabel appearanceWhenContainedInInstancesOfClasses:#[[UITableViewHeaderFooterView class]]].textColor = [UIColor darkTextColor];
we can change the header background color like this :
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
let tableHeader = view as! UITableViewHeaderFooterView
tableHeader.backgroundView?.backgroundColor = UIColor.white
}

iPhone Fixed-Position UITableView Background

I'm building an iPhone app without the use of Interface Builder. I have set the background of a grouped UITableView in the following manor:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"groupedBackground.png"]];
I'm trying to fix this background image so that it doesn't scroll with the table cells. Does anyone know how to do this?
We find that it works exactly as you ask, if instead of using backgroundColor, you assign to backgroundView, like so:
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"background.png"]];
The table cells then scroll on top of the background, which is stationary. (This has been available since version 3.2 of the SDK, I believe.)
You can achieve a stationary background using a pattern image as follows:
UIView *patternView = [[UIView alloc] initWithFrame:tableView.frame];
patternView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"groupedBackground.png"]];
patternView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tableView.backgroundView = patternView;
A little late but maybe for all the others looking for a solution. I could get that work by calling the parentViewController in the viewDidLoad method of the UITableViewController:
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
self.tableView.backgroundColor = [UIColor clearColor];
or with using just a background color:
self.parentViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
self.tableView.backgroundColor = [UIColor clearColor];
It took me a while to figure that out but now it works like a charm.
You can place an additional view below UITableView and set its background, so if UITableView is transparent - you'll achieve your goal - it will have correct background and it will not scroll.
use this below code in swift 3.0 to achieve constant background for tableview
self.tableView.backgroundView = UIImageView(image: UIImage(named: "background.png")!)

How to set table background transparent in iphone?

I am new to iphone development .I have displayed a list of contents in a grouped table view.How can set the table background transparent such as i should see the text displayed on the gray-color(default color) background and not on the white color.Please help me out.Thanks.
cell.backgroundColor=[UIColor clearColor]
This worked fine for me.
table.backgroundColor = [UIColor clearColor]
Marco
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = nil;
table.backgroundView = nil;
This works for me.
In iOS 6 seems this is not enough. I have to set both,
menuTable.backgroundColor = [UIColor clearColor];
if ([menuTable respondsToSelector:#selector(setBackgroundView:)]) { // only after 3.2
[menuTable setBackgroundView:nil];
}
self.view.layer.backgroundColor = [UIColor clearColor].CGColor;
Works on iOS 9.
UITableView inherits a 'alpha' property from UIView so try changing that :)