Separator lines appearing around UITableViewCell on select/highlight in iOS8 - ios

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)

Related

Hiding separator for empty cells doesn't show separator at all

In a table view I'm hiding separator for empty cells only by following code which does hide separator for empty cells but it also doesn't show separator at all. I can't figure out why, any clue?
Alternatively is there any other way to hide separator for empty cells?
There is nothing wrong with the following code, it does work as intended on other VCs but on this particular VC it doesn't work as intended. I've checked storyboard and compared working and non working VCs. Both have exactly same attributes for the tableview.
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
You can do like this:
tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
or :
tableView.separatorColor = .clear
You can use this method:
tableView?.tableFooterView = UIView(frame: .zero)
It will still render separator but with empty frame, so it will not be visible for empty cells only, while original style will remain for filled one's.

How to set UITableView cell separator to 'None' when selected

I am working on a UITableView with customized cells from a .xib. I have added a blue border to the top and bottom of the .xib to give a customized spacing between the cells. I don't want a separator between the cells so I set the Separator to None in the storyboard. This is the look that it gives me.
This works great until I select a cell, at which point I am seeing this (notice the white separators above and below the selected cell):
I am using this code in the cellForRowAt function to set the color of the selected cell to white, but this also seems to force the separator to be white:
let selectedView = UIView()
selectedView.backgroundColor = .white
cell.selectedBackgroundView = selectedView
I am trying to figure out how to remove the separator lines when the cell is selected. I have tried several things on the storyboard and in code, but haven't found anything that works.For example:
I can't use cell.selectionStyle = .none because I want the cell background to turn white when selected.
This doesn't change anything when called from viewDidAppear: tableView.separatorColor = UIColor.clear
I tried the answer here Hide separator line on one UITableViewCell by Avinash but it didn't do anything.
Any ideas?
I found that this solution worked for me.
I changed my code in the cellForRowAt function to the clear color:
let selectedView = UIView()
selectedView.backgroundColor = .clear
cell.selectedBackgroundView = selectedView
try change the tableView.separatorColor = tableView.backgroundColor
and set you table style to grouped

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)

set uitableview background view

I want to set the background view for my tableview,
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let backgroundView = BackgroundGradient(frame: self.view.frame)
self.tableView.backgroundView = backgroundView
}
however the background remains white (it should be a blueish gradient). I have tried to set it in view did load, same result, I changed to background color to clear, and still nothing. what am I missing?
Your UITableViewCell could be obscuring the backgroundView of your table. Try setting your cell's background color to clear.
Quote from Apple Docs (https://developer.apple.com/reference/uikit/uitableview/1614986-backgroundview)
You must set this property to nil to set the background color of the table view.
Try using:
self.tableView.backgroundView = UIImageView(image: UIImage(named: "backgroundImage.png"))
See if this works then there must be some issue with backgroundView
i found the problem... i had forgotten that UIColor(red:green:blue:alpha:) takes CGFloats between 0 and 1 instead of 0-255 as usualy, fixed it and it works fine now... Thanks all

UITableView backgroundColor always white on iPad

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

Resources