Container View White Background, iPad Swift Issue - ios

My problem only occurs on iPad. By default, my container view is clear/transparent. It works and appears fine on iPhone but when displaying on iPad it defaults to a white background. The issue is the same on most custom uitableviews as well.
I've attached an image of the problem below:

This was basically a solution to my problem, only thing I had to add and I will attach the swift code was "willDisplayCell" method using tableViewDelegate.
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
var backgroundView : UIView = UIView(frame: CGRect.zeroRect)
backgroundView.backgroundColor = UIColor.clearColor()
cell.backgroundView = backgroundView
cell.backgroundColor = UIColor.clearColor()
}

It is problem with cell's background color on iPad. I'd faced with this problem.
I've fixed this problem by changing background color of all components of cells to the clear color in code.
//in cell's awakeFromNib
UIView* backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
backgroundView.userInteractionEnabled = NO;
backgroundView.backgroundColor = [UIColor clearColor];
self.backgroundView = backgroundView;
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
I think this is very helpful

The Default colour for cell's ContentView is different for iPhone and iPad.
You have to set Background colour of Cell's ContentView in your Storyboard and you are done.

Related

How to prevent UITableViewCell from being visually deselected right at tap?

I use the following to set the selected backgroud color of my UITableViewCells:
let backgroundColorView = UIView()
backgroundColorView.backgroundColor = .red
UITableViewCell.appearance().selectedBackgroundView = backgroundColorView
When I tap to select them, the cell briefly flashes red and then return to the backgroundColor I've set.
When I add:
if cell.isSelected
{
cell.backgroundColor = .red
}
to cellForRowAt indexPath, the cell becomes red when I scroll it out of sight and back again.
How can I make sure that the cell remains red immediately after I tap it?
EDIT:
If I add the following code to cellForRowAt, things work as expected:
let backgroundColorView = UIView()
backgroundColorView.backgroundColor = .red
cell.selectedBackgroundView = backgroundColorView
Could be an iOS bug?

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):

Change Press Down Color of UITableView

When I tap or hold one of the cells in the UITableView the color of the cell becomes gray by default. I want it to be some other color. I looked everywhere in the storyboard and on stack overflow but could't find it. How do we do it.
Try to change it like this, which will be red for example:
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor.redColor()
cell.selectedBackgroundView = bgColorView
This is an extension based to #evgeny-karkan answer
extension UITableViewCell {
func ChangePressDownColor(to Color: UIColor){
let bgColorView = UIView()
bgColorView.backgroundColor = Color
self.selectedBackgroundView = bgColorView
}
}
Usage:
cell.ChangePressDownColor(to: .red)
check the selection after selecting the cell, where it currently says default.
however, this is very limited number of selection.
if you want custom, you should do it programatically.
i.e./ cell.selectedBackgroundView = customView with color

UITableView background color and separator style change not working

I tried to change the background and separator style in a UITableView, but nothing happens. In the snippet below, everything besides those changes works.
This tableView is located in a #IBDesignable UIView. Best part is, that everything shows as it should in the interface builder. The colors change, the separators dissapear.
func getTableViewToDisplayRecords() -> UITableView {
let tableView:UITableView = UITableView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: self.frame.size.width, height: (self.frame.size.height * 3) / 4)), style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView(frame:CGRectZero)
tableView.backgroundColor = UIColor.clearColor()
tableView.separatorStyle = .None
return tableView
}
What could be causing this behavior?
----- EDIT -----
I have managed to do a workaround:
UITableView.appearance().backgroundColor = UIColor.clearColor()
UITableView.appearance().separatorStyle = .None
Now everything works fine, although this is not really a solution of the problem. Any new ideas anyone?
Try the code snippet below to change the background colour of UITableview along with no separator between the table cell's.
{
tableView.backgroundColor = UIColor.clearColor()
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
}
Also, you need to change the background color of UITableViewCells
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
// change the background color of cell, which is by-default white
cell.backgroundColor = UIColor.clearColor()
return cell
}
Here is the screenshot with the table background color being red (for demonstration purpose) and no seperator between cell.
And here is another image with table background being red and single line seperator between cell.
Hope this helps!!
I think this is a Bug in UITableView.
When your tableView move to superview, sometimes the 'separatorStyle' property will reset to UITableViewCellSeparatorStyleSingleLine(the default style).
So you must set this property again when you finish frame change. Like this:
[self.view addSubview:_searchContentView];
_searchContentView.separatorStyle = UITableViewCellSeparatorStyleNone;

Custom cell clear background?

I have a tableview in a view controller that is dynamically populated with data from a database. Now I have set the tableview to be clear and it working correctly, but I have tried to set the cells to be clear to no avail ?
cell.backgroundColor = UIColor.clearColor()
That line has been placed in the cellForRowAtIndexPath function.
This little extension should help you.
The idea is to set the backgorundView to clear too and not just the backgroundColor:
extension UITableViewCell {
func setTransparent() {
let bgView: UIView = UIView()
bgView.backgroundColor = .clearColor()
self.backgroundView = bgView
self.backgroundColor = .clearColor()
}
}
Usage:
In the cellForRowAtIndexPath add the following line:
cell.setTransparent()

Resources