UITableView backgroundColor always white on iPad - ios

I'm working on a project. I have plenty of UITableViews which are set as clear color. Their views' background color are set to my custom color and everything is fine on iPhone.
The issue comes up on iPad! I tried almost everything, but my UITableView has a white color.
I checked the other topics, like: UITableView backgroundColor always gray on iPad, but nothing worked. Also, my problem is not grey, it's white as snow!
What might be the reason of it?

Good News: According to the release notes, for iOS 10:
When running on iPad, the background color set for a UITableViewCell
in a Storyboard is now respected.
For versions <10:
I was seeing this in iOS 8 (8.3). Even though in IB my cells were "clear color" and their content views were "clear color" they would render as white. An imperfect but reasonable solution, since it still takes values from IB:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
cell.backgroundColor = cell.contentView.backgroundColor;
return cell;
}
It seems that my dequeued reuseable cells get their background forced to white on iPad. I was able to determine this using the view hierarchy debugger.
Once I did this I was able to use the table's background color and didn't have to set a background view, although that works as well.

You can fix this by making an appearance API setting in your appDelegate file :
Swift:
UITableViewCell.appearance().backgroundColor = UIColor.clearColor()

Instead of setting the background color, trying using a background view instead, like this:
- (void)viewDidLoad {
self.tableView.backgroundView = [UIView new];
self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
}
I've had problems where using the backgroundColor doesn't always produce an effect, but setting a background view instead works fine.

Building off of Ben Flynn's answer... cell.contentView background color is not necessarily equal to the cell.background color. In which case, I found that this worked in resolving the iPad white background issue in more situations:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
cell.backgroundColor = cell.backgroundColor;
return cell;
}
While the statement looks ridiculous and crazy... it resolves the issue.

Swift 5
I have a table view with many different cells inside, each one has different color.
The answer from #Ben Flynn cell.backgroundColor = self.contentView.backgroundColor can not achieve that.
The reason is self.contentView.backgroundColor is nil, so what you did is just clear the cell.backgroundColor = nil.
Basically it is the bug from Xcode (I think, yeah it sucks!), cell.backgroundColor still has color, but it can not display.
After debug for a while, based on #Ray W answer, here is my solution.
class YourCustomCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = backgroundColor // Tricky to re-apply the background color
}
}

This solve this issue for me
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
tableView.backgroundColor = UIColor.clear
}

In my experience, some versions of iOS set UITableViewCell's backgroundColor before calling the delegate's tableView:willDisplayCell:forRowAtIndexPath:. Resetting back to your custom color in that method fixes it.

Swift 3.1
I've worked around this bug by placing this in my UITableViewCell subclass:
When the cell is being loaded from the NIB file:
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = UIColor.clear
}
and when the cell is being reused by the system
override func prepareForReuse() {
super.prepareForReuse()
self.backgroundColor = UIColor.clear
}
iOS 10 is supposed to fix this issue in Interface Builder, as animeshporwal said.

SWIFT 3.XX
Put this
UITableViewCell.appearance().backgroundColor = UIColor.clear
In AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool

I'm using Storyboard with UITableViewControlrs, so the most simple decision was to subclass all controllers, and add this method to parent
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
cell.backgroundColor = [UIColor clearColor];
}
}

SWIFT
This was a happening to me, too. My table view in my SWRevealViewController appeared white on my iPad when it looked clear (which is how I wanted it with a background image) on my iPhone. I tried all of the above but this is what ended up working for me in my viewDidLoad().
tableView.backgroundView = UIImageView(image: UIImage(named: "gray"))
tableView.backgroundView?.backgroundColor = .clearColor()
UITableViewCell.appearance().backgroundColor = .clearColor()

This can be achieved in a Storyboard as follows:
Show the document outline for your storyboard
Within your table, pick a TableViewCell
go to the Content View within that Cell
Set the background colour of the Content view.
Result: iPad and iPhone simulator views look the same

I just had this issue and fixed it by changing the backgroundColor of the View (as opposed to the one of the tableView). Seems on iPad, that's the one that is used first and in my case it was set to white.

SWIFT
I had this problem too. Works fine on .phone but tableViewCells go white on .pad. Thought I'd show how I fixed it using swift.
Connect The tableViewCell to the viewController.swift as an #IBOutlet like so:
#IBOutlet weak var tvc1: UITableViewCell!
#IBOutlet weak var tvc2: UITableViewCell!
#IBOutlet weak var tvc3: UITableViewCell!
Then in viewDidLoad put the following:
tvc1.backgroundColor = tvc1.backgroundColor
tvc2.backgroundColor = tvc2.backgroundColor
tvc3.backgroundColor = tvc3.backgroundColor
Very strange, I don't know whats happening here but this solved it for me.

This seems to be fixed with iOS 10 Beta 4 as mentioned in release notes under UIKit notes:

I have a transparent table view with semi-transparent table view cells. I set the table view background color to clear when I create the table. This works for all iOS/device combinations except iPad + iOS 8, where the background color remains white.
For me, setting the cell's background color to semi-transparent and the content view background color to clear works, since I do it on each tableView(_ tableView: UITableView, cellForRowAt indexPath). The problem for me was only that the table view background remained white.
I tried all combinations that I did find regarding this issue, but the only thing I actually needed to to was to set the table views background to transparent on each tableView(_ tableView: UITableView, cellForRowAt indexPath). Not super-intuitive, but at least it works. :P

Related

UITableViewCells bottom edge flickering when app enters foreground

I've build an app which contains an UITableView with a bunch of cells. Inside the cells I've got a view, which fill the whole cell. I've configured the tableview like this:
tableView.separatorStyle = .none
tableView.backgroundColor = UIColor(red: 24/255.0, green: 34/255.0, blue: 41/255.0, alpha: 100)
tableView.separatorColor = UIColor(red: 26/255.0, green: 34/255.0, blue: 40/255.0, alpha: 100)
Whenever the app enters the foreground, I got those little lines flickering for 0.5 seconds or so. To be clear, I don't want those.
And this is how it looks like when the app fully entered the foreground, and how it is supposed to look like:
Any ideas how to get rid of them?
EDIT 1:
I'm starting to doubt that the flickering is related to the separators, because it is only happening between cells in a section, not between the section-cell and the first cell in a section. I've grabbed some screenshots of the view hierarchy and the constraints related to the view (Foreground view) I show in the cell.
EDIT 2:
If I set the top and bottom constraint to -2 instead of 0, there's no flickering at all, however it's not as I want it visually. So the flickering is not related to the separators at all.
Trick for removing the cell separators.
Objective-C
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.tableFooterView = [UIView new];
}
Swift
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
Usually flickering happens when you're returning a wrong heightForRowAtIndexPath.
In your case, you're returning a little smaller than your cell's actual height I guess.
So try to set "clipToBounds" of your cell to "true" and check if it works.
Try setting "Renders with edge antialiasing" to YES in your info.plist.
I think here your issue with UITableViewStyle. Right now you are using UITableViewStyle Grouped. So, line between cell isn't UITableViewCellSeparator it's Group Table 1 pixel header and footer space. So,
I have two solutions:
Either use UITableView background color same as cell background color.
Change UITableView style to Plain
GroupTable SS
or
PlainTable SS
I hope it'll help you. And solve your issue :)
I think it's not related to the separator, because the separator doesn't cover the whole screen, it must be related to your constraints, try changing the background color of the BackgroundContainerView, the DepartureCell and the TableView, one of these 3 views should have the dark grey color as a background color.
Would it be possible that the tableview is inherited from another one and seperatorStyle could be set different in the super class? Then, you need override it.
Set this in viewDidLoad()
tableView.separatorStyle = .none
You can do it as per follows, from your storyboard to avoid that separator from UITableView.
If you set
tableView.separatorStyle = .none
on viewDidLoad(), it will flicker
you need to it before like in viewWillAppear() or in the storyboard
Try setting the tableview separator color with full transparency it might help
tableView.separatorColor = UIColor(red: 26/255.0, green: 34/255.0, blue: 40/255.0, alpha: 0)
Do this in viewWillAppear
If that will not help check the view hierarchy maybe there is an issue with the cell rendering in
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
Check this post and its swift 4 update
or try adding this extension to your ViewController
extension UITableViewCell {
func removeCellSeparators() {
for subview in subviews {
if subview != contentView && subview.frame.width == frame.width {
subview.removeFromSuperview()
}
}
}
}
then try calling it in just before you return the cell
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath)
cell.removeSeparators()
return cell
}
if this doesn't help then please post more information about your setup, code or maybe host a minimal version of your app with the tableView having the issue on gitHub so that I can help.
EDIT 1:
Try setting the rowHeight / cellHeight to 15 pixels more than what it currently is if that will solve your problem than the cellHeight is what needs tweaking could be that it only needs to be 2-4 pixels higher. Probably as the app is entering the foreground autolayout is trying to do what it can do show everything as you want however some constraints are ambiguous therefore whilst entering from the background there is the view appearing animation from the system for about half a second and there is your flickering as well.
Can you use Xcode "Debug View Hierarchy" to find question View , and use "KVC" remove that view.
ps. my english is poor , i hope i can help you
From storyboard select table view separator to None.
In Separator Inset select custom and remove left value make it from 15 to 0.
Build and run it again now check.
Make tableView's backgroundColor and separatorColor exactly the same as in:
tableView.backgroundColor = UIColor(red: 24/255.0, green: 34/255.0, blue: 41/255.0, alpha: 100)
tableView.separatorColor = UIColor(red: 24/255.0, green: 34/255.0, blue: 41/255.0, alpha: 100)

UITableViewCell reordering style in iOS 11

I have a UITableView which allows reordering. In all iOS versions before 11 the cell that is currently reordered is shown properly (just slightly transparent) while dragged.
In iOS 11 instead of the actual cell content, I see a white semi-transparent rectangle.
I searched everywhere but could not find a way to show the actual content. Any ideas anyone?
It seems that the problem was that while the cell is being dragged, all subviews of its contentView get a transparent background.
I fixed it by keeping a reference (as an IBOutlet) to my own background view and setting its backgroundColor in the cell's layoutSubviews. You can do the same for all other views whose backgroundColor you want to preserve.
class QuestionOrderTableViewCell: UITableViewCell {
#IBOutlet weak var bgView: UIView!
override func layoutSubviews() {
super.layoutSubviews()
bgView.backgroundColor = UIColor.black
}
}

UITableViewCell selection style changing background color for all subviews

I'm writing in swift, using the storyboard, and I'm working with iOS 10.
I have a UITableViewCell composed of some subviews (they're UIViews). Some of those subviews have a background color.
The selection style on the cell is Default (gray).
When I tap the cell, the cell becomes gray. Good. However, the background color of each subview in the cell is also changed automatically to match the selection color (gray) of the cell. Not good.
How can I best prevent this behavior? I don't want the background colors of the subviews in the cell to change.
NOTE: I don't consider it a very good solution to do my own selection by listening for the didSelectRowAtIndexPath delegate method and then setting the background color of the cell's content view. Perhaps this is the only way, but I want to see if there are other options first.
UPDATE:
Just to clarify, another reason I don't like the above solution of listening for the delegate is that it fixes the issue per cell. So if I have other custom cells that use the same subviews, I would have to implement the same fix. I want something that fixes this at the view level, rather than the cell level. So that when I use those same views in other custom cells for different table views, I don't have to worry about it.
This way is better than listening to the didSelectRowAtIndexPath for sure.
Play with these properties in your custom cell class:
override var isSelected: Bool {
didSet {
if isSelected {
//play with colors
print("selected")
} else {
//play with colors
print("deselected")
}
}
}
override var isHighlighted: Bool {
didSet {
if isHighlighted {
//play with colors
print("highlighted")
} else {
//play with colors
print("not highlighted")
}
}
}
Disable selection style:
cell.selectionStyle = UITableViewCellSelectionStyleNone
And tricky one:
cell.selectedBackgroundView = UIView()

Unable to make UITableViewCell have clear color

This is very straightforward. I've tried this a couple of times, in both tableView:willDisplayCell:forRowAtIndexPath: and tableView:CellForRow:atIndexPath:.
Here is my code:
cell.alpha = 0.0
cell.backgroundView?.alpha = 0.0
cell.contentView.alpha = 0.0
cell.backgroundView?.backgroundColor = UIColor.clear
cell.backgroundColor = UIColor.clear
cell.contentView.backgroundColor = UIColor.clear
I'm pretty sure this should be overkill, but I'm having a really hard time making this work. The cells are being modified inside an if statement because this only applies to certain cells. Yes, I've checked, and the if statement is working as expected. Any ideas?
I am using Xcode version 8.2 beta
You should set:
cell.backgroundColor = .clear
and also you should provide clear color for whole tableView:
tableView.backgroundColor = .clear
I've tested in sample project. I create UIViewController with UITableView. Set the .red color for view of UIViewController. Create UITableView, apply .clear color for cell and tableView. You should see the red colored background - so the cells and tableView are transparent.
Here are example screen(The label is added to .view of UIViewController to make sure that tableView is transparent):

Separator lines appearing around UITableViewCell on select/highlight in iOS8

I have a very simple UITableView where I have set the separator inset style to be none:
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
Running the app I get what I expect with no lines between the cells. However, when I select one of the table view cells, white lines appear around it. Why is that and can I change the appearance of it? Screenshots below:
The lines shown by arrows appear on highlight. The cell colour change is intentional. Any help in the right direction would be much appreciated. I am using iOS 8.1 as target with XCode 6.1.1
I have set the background colour as follows.
In viewDidLoad:
tableView.backgroundColor = UIColor.blackColor()
tableView.alpha = 0.8
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
In cellForRowAtIndexPath:
thisCell.contentView.backgroundColor = UIColor.blackColor()
thisCell.contentView.alpha = 0.85
I'm assuming that you are overriding setSelected and setHighlighted?
self.selectedBackgroundView.hidden = true
works for me.
It took me a while to find a solution to it as well. And this is the view hierarchy that I found for the cell:
0: class: 'UITableViewCellSelectedBackground'
0: class: 'UITableViewCellContentView'
0: class: '_UITableViewCellSeparatorView'
The UITableViewCellSelectedBackground is setting the background. The background is then overlayed with the ContentView.
Could you please say how you made such background in cells? In my opinion it's not selection lines, it just whole grey background, that appear under you cell black backgrounded view while section or highlighting. You could use debug view heresy or just override this two functions:
func setSelected(selected: Bool, animated: Bool)
func setHighlighted(highlighted: Bool, animated: Bool)

Resources