ios Change background of static TableView - ios

I'm trying to put a background image into a UITableViewController with static cells inside.
Usually, in normal UIViewControllers I do this by changing the UIViewController's background color and setting the tableview's background color to clearcolor and opaque to NO
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"defaultBackground.png"]];
self.tableView.opaque = NO;
self.tableView.backgroundColor = [UIColor clearColor];
but doing this in my UITableViewController with static cell's leaves me with a transparent tableview with a black background. It seems like there is no view underneath.
I tried to add the view and make it contain the tableview, but this leaded to an empty view without the tableview.
self.view = [UIView alloc]initWithFrame:self.tableview.frame;
[self.view addSubview:self.tableView];
[self.view bringSubviewToFront:self.tableView];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"defaultBackground.png"]];
self.tableView.opaque = NO;
self.tableView.backgroundColor = [UIColor clearColor];
Is there a way to fix this?

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];

UITableView weird separator line color

i have a problem in setting UITableViewCell background color. I wanna change it into my own color, so I use this code to change that background color :
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [ColorManager backgroundInput];
bg.layer.masksToBounds = YES;
[cell setSelectedBackgroundView:bg];
I implement this code in CellForRowAtIndexPath method.
The weird thing I have is, when i tap into a cell, the separator line color being highlight just like the image below. I just wanna make it still dark, anybody have idea?
Thank you
After setting up your own background view,which will have the line or not,
you can clear the default tableview color
tablename.separatorColor = [UIColor clearColor];
OR
If you don't want to remove the default line then you can give same color of background view to line color
//You can specify the RGB of background view
tablename.separatorColor = [UIColor colorWithRed:R green:G blue:B alpha:1.0f];
I think the best option is
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Another option is to set the backgroundColor of the UITableViewCell to a clear color.
cell.backgroundColor = [UIColor clearColor];
try it
Objective-c
tablename.separatorColor = [UIColor blackColor];
Swift
tablename.separatorColor = UIColor.blackColor();

How to get rid of the table cell's selectionStyle but still recieve setSelected/setHighlighted calls

I've tried the method presented here, and everything works except that I cannot make the selectedBackgroundView transparent, it stays white. In my custom UITableViewCell class I have:
-(void)awakeFromNib {
UIView *bkgView = [[UIView alloc]initWithFrame:self.frame];
bkgView.alpha = 0.5;
bkgView.backgroundColor = [UIColor clearColor];
self.selectedBackgroundView = bkgView;
}
I've also called [self setNeedsDisplay]; in the setSelected method.
What I want is to remove the blue highlight (from UITableViewCellSelectionStyleBlue
, I don't want to set this to UITableViewCellSelectionNone as it will disable the setHighlight/Selected calls) and still have the highlight/select methods called. I'm so very nearly there, it's just that the selectedBackgorundView remains white! If I set it to redColor or blueColor, it will appear as red or blue, but when I put clearColor, it shows as white. I thought about setting the background with colorWithPatternImage using a transparent PNG image, but I'd like to avoid this.
I prefer to try and figure this out as it's a lot cleaner to rely on these two method calls, and I also want to retain the use of the disclosure indicator which becomes white when selected, which will otherwise stay dark if I use UITableViewCellSelectionNone.
I'd recommend using tableView:willSelectRowAtIndexPath: and returning nil.
That way you still get notified when a Cell is going to be selected. However, it won't actually get selected, and from there you can manage selection yourself without having to deal with the control's default styling.
you use the selectedBackgroundView to override the blue
in viewForRow: method:
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
v.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = v;
Set the background view and the selectedBackgroundView:
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor whiteColor];
self.backgroundView = view;
view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor clearColor];
self.selectedBackgroundView = view;
Then override - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
self.backgroundView.hidden = highlighted;
}

Setting UITableView backgroundColor in universal app (iOS 5)

I have to set the backgroundColor property of a UITableView to a certain color in my universal app. If I write this...
self.tableView.backgroundColor = [UIColor colorWithRed:146.0f green:197.0f blue:240.0f alpha:1.0f];
...it doesn't work on iPhone nor iPad (background results white).
If I use a standard color, instead...
self.tableView.backgroundColor = [UIColor greenColor];
...it works on iPhone but not on iPad.
I tried other solutions suggested on SO (backgroundView to nil/clearColor, etc.), but none of those works on iOS SDK 5. Can you help me?
You need to divide each color value to 255.
Your color will be:
[UIColor colorWithRed:146/255.0 green:197/255.0 blue:240/255.0 alpha:1.0];
Instead of changing the background color, you can set a backgroundView (with the background color that you like) for the tableView.
UIView *bgView = [[UIView alloc] initWithFrame:self.tableView.bounds];
bgView.backgroundColor = [UIColor redColor];
self.tableView.backgroundView = bgView;
Are you using a navigation controller?
Try this:
self.tableView.backgroundColor = [UIColor clearColor];
[self.tableView setSeparatorColor:[UIColor clearColor]];
self.navigationController.view.backgroundColor = [UIColor greenColor];
Thats what I use with iOS5 and an universal app.

Dark Corners UITableViewController

I have a UITableViewController. In viewDidLoad, I do the following:
self.tableView.backgroundColor = [UIColor clearColor];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg_cafe.png"]];
The background is supposed to be a gradient. Two issues:
Gradient seems to get cut off at the end of the UITableView.
The corners are tinted.
I've experimented with setting the UITableCell's opacity to NO, but that doesn't worked. I have read these threads already: Transparent background in grouped UITableView - iPhone, and Black corners around UITableViewCells.
I'm not using Interface Builder at all for this.
Example:
Make a UIView with that pattern image as background color and set it as table view's background view.
UIView *bgView = [[UIView alloc] initWithFrame:self.tableView.frame];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg_cafe.png"]];
self.tableView.backgroundView = bgView;
[bgView release];

Resources