UITableView - SelectedBackgroundView not working in iOS7 with Auto-Layout - ios

I am using UITableVeiw with static cells in iOS7. The table view looked like this before I converted my storyboard to use autolayouts.
I am using the "background view" property and the "selectedBackgroundView" property of the tableviewcell to set backgrounds like so:
After enabling auto-layout though in the storyboard, the layout goes bonkers and this is what I am left with:
I don't have any auto-layout issues that are presented to me. Just that I am not seeing the foreground and background anymore with auto-Layouts.
Any help?

Seems like the auto-layout was creating problems for me. I added the backgroundView and selectedBackgroundView programmatically and it works.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIView *selectedView =[UIView new];
selectedView.backgroundColor=[UIColor colorWithRed:0.604 green:0.812 blue:0.059 alpha:1.000];
cell.selectedBackgroundView = selectedView;
UIView *defaultView =[UIView new];
defaultView.backgroundColor=[UIColor colorWithRed:0.396 green:0.404 blue:0.404 alpha:1.000];
cell.backgroundView = defaultView;
}

I think it can be fixed in Storyboard but... the "solution" is totally stupid :D. If add width and height constraint to selectedBackgroundView it will works. This solution isn't good for me, but seems like it works :)
P.S. The way with adding selectedBackgroundView programmatically is work for me too.

Related

UITableViewCell will not work with UIColor colorWithRed [duplicate]

I have created a bunch of cells which I reuse in a table view. All of those cells just have different UILabels inside them and some UIImageViews (nothing covers the complete cell).
Setting the background color in IB has no effect (always white or transparent, can't say which one of them). But if I press Command-R (simulate interface) the cell has the correct background color in the simulator.
I tried to set it in tableView:cellForRowAtIndexPath: but it doesn't work like I would think either.
This does the trick:
cell.contentView.backgroundColor = [UIColor redColor];
but these have no effect (even if I set the cell.contentView.backgroundColor to clearColor):
cell.backgroundView.backgroundColor = [UIColor redColor];
cell.backgroundColor = [UIColor redColor];
I set all the layout/font/background stuff in IB. Any idea why this isn't working in this case?
Why do I need to modify the contentView's backgroundColor and not the backgroundView's?
It seems to be a common issue. Could somebody please point me in the right direction to (finally) understand how background colors are handled within a table view cell.
To change the background color of the cell, you have to do it in the willDisplayCell:forRowAtIndexPath: method. This is mentioned in the UITableViewCell documentation near the bottom of the Overview. So you need:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor redColor];
}
The contentView is just the recommended subview to put custom controls in so that the cell gets layed out properly when table editing is going on.
There's an easier solution: When you create a UITableViewCell in Interface Builder, simply drag and drop an additional UIView so that it covers the entire UITableViewCell you're creating within IB. Place your additional UI elements on top of this additional UIView. You can set this extra UIView to whatever background you like.
There are quite a few answers on StackOverflow that suggest you change the background color of your UITableViewCell by using a line of code like this one:
cell.contentView.backgroundColor = [UIColor redColor];
If you add a drop shadow to your cell by adjusting the contentView's frame size slightly, you may find that this line will change both the background color of your cell and the color of your drop shadow area as well.

Change color of inset area in UITableView separator

I have a customized UITableView, the cells have a different background color (set in a custom backgroundView). However, the background color is only applied within the cell, but not extended to the inset area of the separator. As you can see in the screenshot, there is a white area to the left of the colored separator.
How can we change the color of this white line? We would like to make the line "disappear" by setting it to the same color as the cell background. Thanks!
Setting the cell's background colour to the same as the contentView's background colour seems to give the best results.
cell.backgroundColor = cell.contentView.backgroundColor;
That will handle cases where the accessoryView is the wrong colour as well.
So I just had this problem myself, and it's strange because it seems to work fine on some tableviews. I'm assuming that the grey color you are using is the background color of your tableview. If not, this solution may not work.
It seems like cells in iOS 7 don't always pick up the tableview background color when a separator inset is present, so you need to manually set the cell background to clearColor. Interestingly, setting this value in storyboards didn't work for me, I had to do it in code. Add this:
cell.backgroundColor = [UIColor clearColor];
when you configure the cell in this delegate function:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
It's the default in iOS 7. But you can change it to the way iOS 6 looks. Please try my code below. You will be amazed:
tableView.separatorColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
NSString *iosversion = [[UIDevice currentDevice] systemVersion];
int version = [iosversion intValue];
if(version>6)
{
tableView.separatorInset = UIEdgeInsetsZero;
}
Swift 3+:
tableView.separatorInset = UIEdgeInsets.zero
Use image for separator. This the only easy way to acechive your expected result. For ios 7 in xib we have edge insect property is there you can use.
Go to storyboard > Attribute Inspector > Separator Insets, then select cutom and change left coordinate from 15 to 0.
it will be Done.
In Interface builder on the UITableViewCell set the attribute 'background' to be clear (or whatever) and then on the Content View set the attribute 'background' to be clear as well and the problem should go away.
This is the only method that worked for me... (separator = None via storyboard)
I had similar issue and below code worked like charm.
[self.tableView reloadData]
reload tableview on didSelectRowAtIndexPath Method.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView reloadData]
}
only this fixed my issue.

UITableView - transparent background in iOS 7

I am checking my app with ios 7 beta. I have set background color of UITableView to clear color. Its still showing white background.
Is there any other way around to make it transparant?
I couldn't make it work but the following worked perfectly :
-(void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
Setting tableview backgroundColor to clear color working absolutely fine in iOS 7 chk screenshot see those separator lines for the table cells while the tableview is transparent.
Believe it or not, the following statement, added to cellForRowAtIndexPath, will cause iOS 7 to honor the XIB-specified color:
cell.backgroundColor = cell.backgroundColor;
In my XCode 5 Storyboard I had to set the background color of the 'View' section on the Table View properties to clear as well.

UITableView with scrolling background pattern

I have a UITableView with the backgroundColor property set like so:
tableview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"IMAGE"]];
This gives the tableview a background, that scrolls, which is exactly what I want.
The problem is, when I add cells to the tableview, in each cell the background pattern of the tableview starts new / starts over:
In the above image, you can see the problem in the red circled areas.
For the tableview cells, I have set all backgroundColors to clearColor for
backgroundView
textLabel
detailedTextlabel
contentView
I have also tried to set
cell.backgroundView = [UIView new];
cell.backgroundView.backgroundColor = [UIColor clearColor];
and the same for the contentView, too.
Additional Note:
Setting the backgroundView of the tableview does not help, since then the background does NOT scroll anymore!
Just ran into this issue and found this fixes the cell having it's own copy of the backgroundColor
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor clearColor]];
}
Here is what I would do:
1) Create a view controller and set the view background color to your pattern image.
2) Add your table view controller as a child view controller to the newly created view controller. This can be done in code or if you are using iOS 6 and storyboards, with an embed segue. If you are doing it in code you may need to set the frame of the table view controller manually.
3) Set the background color of your table view controller to [UIColor clearColor]
I had a similar issue and this is what worked for me.

Why doesn't UITableViewCell background color work (set in interface builder)?

Why doesn't UITableViewCell background color work (set in interface builder)?
I note from some searching that the follow code set in your custom subclass of UITableViewController does work (see below):
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
But I would still like to just understand why interface builder has a background color setting for the TableViewCell, which effectively doesn't seem to work?
It's a bit late, but I just ran into this issue... Setting the background color on the contentView works fine:
cell.contentView.backgroundColor = [UIColor redColor];
What worked for me is creating my own UIView object to be set as the background view of the UITableViewCell. In the cellForRowAtIndexPath:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor orangeColor];
[cell setBackgroundView:bgview];
Trying to set the background colour of the UITableViewCell itself is not how you should be doing it. A table cell has a collection of five useful views you can access, one of which is backgroundView.
Recommended reading:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
and then:
http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html
You should always provide changes for cells in tableView:willDisplayCell:forRowAtIndexPath: instead of the tableView:cellForRowAtIndexPath: method, as per Apple Documentation.
This Works !!
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor redColor]];
Make sure before defining a cell color your backgroundView is null
cell.backgroundView = nil;
cell.backgroundColor = [UIColor redColor];
This is simple and works: in interface builder, add a view object to the UITableViewCell of the same dimensions, and place your controls inside that view. Then you get whatever background color you want.
Edit: here's a reason to accept, it just struck me: take a careful look at what you get when you double click on a UITableViewCell in IB that you place into your nib, to start adding stuff to it: you don't get a plain rectangle looking view, you get an oval rectangle that says quite clearly: Content View. So all you can do is set the content view of the UITableViewCell in IB, and setting the background color property of the content view does not produce the color change in the table. As the docs say, UITableViewCells are complex beasts with many parts with a lot going on behind the scenes in terms of the UITableView adjusting it and fiddling with it.
It works perfectly, if you pick the tableview and set background to for example red the whole table will be red.
maybe you do not linked the table or something
hope it helped
(try this)
Apple's IB's design is generic to be applied to multiple kinds of components, and is also targeted at a highly technical audience (developers). Hence it has not been designed (as an application) to fully customise the interface properly for each component/situation type. So in this case it is the case the ability to seemingly set the background-color is somewhat misleading.
Y not, use setBackgroundColor
self.table.separatorColor=[UIColor whiteColor];
[table setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
You can partially do this in Interface Builder (probably added to Xcode recently):
Expand the cell in the Storyboard's Document Outline and select "Content View"
Set Content View background color in the Attributes Inspector
Select each content item in the cell and set their background colors
to ClearColor
Still don't see a way to set the accessory background though.
I did get this issue also.
In IB you get the ability to change the background color of the cell when choosing cell. This is NOT correct. You should go to the document outline on the left of the storyboard and choose the content view inside the cell. Change the background of the content view. You should now get correct cell color.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:view];}
We need to change the color in this method.
Try this it works for me:
- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: #"Cream.jpg"]];
}

Resources