Change UITableView background color - ios

I know this question has been asked before but all the solutions I could find failed, I tried using "background = nil" method, I've tried override func UIColor and non of these methods work. When I run my app it has a red background but all the cells in the table view have white backgrounds. I am not using any prototype cells. I've also changed the background color in view and section index.

You can try use "cell.backgroundColor" in cellForRowAtIndexPath. ˆ_ˆ

Please try this :-
cell.backgroundColor = UIColor.clearColor()
in the UITableViewDataSource protocol implementation
cellForRowAtIndexPath

Related

Setting UITableViewCell selected background color with UIAppearence not working

I want to change the selected color of all my UITableViewCells. I am already using UIAppearence to define basic properties for some other UI elements and it works.
However, setting
let bgColorView = UIView()
bgColorView.backgroundColor = #colorLiteral(red: 0.9411764706, green: 0.9411764706, blue: 0.9411764706, alpha: 1)
UITableViewCell.appearance().selectedBackgroundView = bgColorView
Results in a strange glitch, the cell losing the selection color, although the UITableViewCells is still selected:
The glitch is not caused by UI code because setting the selected color directly in UITableViewCell.awakeFromNib works fine, the selected cell stays the desired color.
Edit: This happens with all the different cells. The cells do not do anything interesting, just set stuff like label colors, etc in awakeFromNib and some RxSwift binding in a model setter to set the values of the various labels. The VCs just react to the cell getting selected by calling a delegate method (using modelSelected from RxSwift).
Edit: The VC with the UITableView is the "left side" of a UISplitViewController. The "right side" is a UINavigationController and navigation in this UINavigationController using setViewControllers after tapping the cell causes the strange glitch.
So after some investigations I was able to reproduce your "glitch" :) I downloaded RxSwift and then tried their tableView sample. I use RxSwift too but I don't use their tableView delegate/datasource extensions. I also made a custom tableViewCell.
After overriding the setSelected method of the UITableViewCell, and not calling its super method, the same case happened. Actually, even without using RxSwift, this should happen.
So again, make sure in your tableView class, if you do not need to do anything with setSelected, don't override it. Otherwise, when overriding super class methods, make sure you're calling the super.method(), just like what you're doing in your controller's viewDidLoad().
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
It should work perfectly fine, I have tried in an iOS application.
Try using a different color for the selected background, It might be same as you cell's background color
And verify that UITableViewCell#selection property is not set to none

Swift 4 backgroundColor bug?

I have a problem with transparency in my Views, especially with UITableView.
Just to make sure. I already have 3 years of experience with swift and this is the first time, I am getting this problem.
So in apple's docs it says the following:
backgroundColor
The view's background color.
Changes to this property can be animated. The default value is nil, which results in a transparent background color."
But when applying clear color to backgroundColor or tableView.backgroundColor = UIColor.clear, the background is still appearing white.
Is this a bug or a feature?
EDIT:
The tableView isn't the problem anymore. It is the cell within the tableView.
Already tried solutions are clearing the background color of all subviews. Nothing changed.
EDIT 29.09.17
Ok, I debugged my application and got this:
UICachedDeviceWhiteColor and cachedColor.
A google searching also didn't help.
Edit 29.09.2017 Later that day ...
The problem occurs, when updating from swift 3 to swift 4.
I made a new project with swift 4, but it actually works fine.
Because it is a problem I have to solve. I will try some things and update this post from time to time.
In UITableViewCell and UICollectionViewCell there is a property view called contentView.
A common mistake is to manipulate the cell directly. Instead try to manipulate the cells content view:
Obj-c
UITableViewCell *cell = ....// get the cell
cell.backgroundColor = ... // BAD !
cell.contentView.backgroundColor = [UIColor redColor] // this is the correct way.
Swift
let cell = ...
cell.contentView.backgroundColor = UIColor.red // make sure its the contentView
also a couple small nuances with colors and opacity:
If you want only the background to have opacity (i.e. less than 100%) , make sure you are not changing the view's opacity (i.e. view.alpha = 0.5 BAD) - change the color's opacity value instead:
i.e.
cell.contentView.backgroundColor = [UIColor colorWithRed:... green:... blue:... alpha:0.5];
Another useful trick is to pause the application and press the "visual debug" button.
Search for your view and check it's attributes in the inspector
Found the problem:
When instantiating a variable which holds the view before displaying it, the background color of this view is white. The debugger calls this UICachedDeviceWhiteColor.
Why would you do that?
If you instantiate the view before it is displayed, you can switch with a menu between different views very fast with "bringSubview(toFront: ...).
How to fix:
To fix this you have to instantiate the view every time you want to show it.
Hope I could help some lost souls with this answer and Apple is going to fix this bug.
Verify that the view behind it is not white, then set the current view's (the one with the transparent background) modalPresentationStyle to overCurrentContext. This will allow views behind to show through the UIColor.clear background color.
If you are setting
tableView.backgroundColor = .red
and if it's an empty TableView then the Red will be visible. I give a headerView and FooterView also to visualise.
Now TableView Contents some data, so to reflect the color you need to set background color to cell.
class UserCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
override func layoutSubviews() {
super.layoutSubviews()
backgroundColor = .yellow
}
....
}
You can check out the original project here.

How can I make the background of UICollection view as transparent without making the cells in it transparent in Swift

I have a collection view and of course I also have the cells in it. If in the property inspector I change the alpha of collection view to 0, the cells in it also become transparent. Is there a way to make the background transparent of the collection view only so the image behind it is visible?
using in Swift 3:
collectionView.backgroundColor = UIColor.clear.withAlphaComponent(0)
You can change the cell colour to clear and set the background to nil to see whats underneath the collectionView. Like this:
collectionview.backgroundView = nil;
collectionview.backgroundColor = [UIColor clearColor];
A UICollectionViewCell's background is defaulted to clear. Simply set a background colour for your cells either in interface builder or in a subclass using:
self.backgroundColor = UIColor.WhiteColor()
if you drop collection view opacity to 0 the whole collection view will be invisible. In your case you want to show collection view cell only so collection view cell's background view should be UIColor.clear either collection view background. Programmatically like this
self.collectionView.backgroundView?.backgroundColor = UIColor.clear
self.collectionView.backgroundColor = UIColor.clear
In my case i need to show chat log like this so i used that code
my example case
In the UICollectionView set the background color to clearcolor in the attribute inspector.
Dropping UICollectionView's Opacity to 0 from attribute inspector will make it transperent.
P.S:
Bad practice, but will work.

Change the color of empty table view cells?

I changed the color of cells from the storyboard (did not use any code) but as you can see in this image:
empty cells stay white as default. I want to change those empty cells to red also but I am having trouble figuring out.
I also in my code setting cell.backgroundColor = [UIColor redColor]; with no success.
You just set tableView's backgroundColor:
yourTableView.backgroundColor = [UIColor redColor];
For others who like to use Storyboard, there's not a specific setting to set this, but in the Table View in Storyboard, you can go to the Identity Inspector, enter a "backgroundColor" and set it as the Color type in User Defined Runtime Attributes:
Not sure why this isn't a setting in storyboard, but this works!
In Swift 3 you can do this:
self.tableView.backgroundColor = UIColor.red
XCODE 8.2 AND SWIFT 3
For change the color for your tableview you can use two ways:
Use (TableViewController): You can call your TableView and change the color via code.
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.backgroundColor = UIColor.red
}
Use (StoryBoard): Select your TableView, and in the inspector tap Atributes then, in the down view find View Section and chosse the color that you want from the Background atribute.

Get rid of line above UITableView

I have a UITableView (which happens to have a UISearchBar) and can't seem to figure out how to get rid of the white/gray border above it. I need to have seamless black between the UISearchBar and the black above it.
I have tried hiding the UISearchBar to see if that had anything to do with it, but the line still appeared.
Any ideas?
In my case it helped to set:
searchBar.clipsToBounds = YES;
You have to customise the UISearchBar background to match according to your requirements.,take a look at this tutorial.
You should try this, by which you can set the color of cell borders and if you want to change the color of a particular cell's border put it in condition as: if (indexPath.row == yourcell):
tableView.separatorColor = [UIColor blackColor];
Also the above method you have to put in CellForRowAtIndexPath method of table view datasource.
Please notify if it works..
This had to do with the pullToRefresh view I was using (using SensibleTableView) - it had some code in drawRect to draw the line.

Resources