How do I make this UITableView and it's cells clear in Swift 3.
I have gone through the previous threads but I am still getting a white background.
As you can see from my code I have tried the various methods mentioned:
override func viewDidLoad() {
self.communitiesTableView.delegate = self
self.communitiesTableView.dataSource = self
let background = CAGradientLayer().bespokeColor()
background.frame = self.view.bounds
// view.addSubview(background)
super.viewDidLoad()
// Do any additional setup after loading the view.
}
and in my cell table function:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let title = self.communities[indexPath.row]
let cell = UITableViewCell()
cell.textLabel?.text = title
cell.textLabel?.font = UIFont(name: "Avenir", size: 12)
cell.textLabel?.textColor = UIColor.red
cell.textLabel?.backgroundColor = UIColor.clear
cell.contentView.backgroundColor = UIColor.clear
communitiesTableView.backgroundColor = UIColor.clear
cell.layer.backgroundColor = UIColor.clear.cgColor
return cell
}
This gif shows the problem - notice the black lines showing the table is present just not populated (as no-one is logged in)
But for a second it is clear, then turns white.
Where am I going wrong?
This is from another post that I have found:
Apple document says
... In iOS 7, cells have a white background by default; in earlier versions of iOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate.
You might need to use willDisplayCell UITableView delegate method to have a transparent background for your table view .
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
How do I apply the code above as it says its for iOS 7?
Note: Below code been tested in Swift 3.
Method 1:
Select your tableViewCell from your storyboard and goto Attributes Inspector under View change Background to clear
Method 2: Try below code inside your cellForRowAt
cell.layer.backgroundColor = UIColor.clear.cgColor
Note : If above method didn't works.try clear your project build by pressing shift + option + command + k
Update: Update your cellForRowAt from below code...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
cell.textLabel?.text = communities[indexPath.row]
cell.textLabel?.font = UIFont(name: "Avenir", size: 12)
cell.textLabel?.textColor = UIColor.red // set to any colour
cell.layer.backgroundColor = UIColor.clear.cgColor
return cell
}
You can try this
In viewDidLoad:
tableView.backgroundColor = UIColor.clear
In cellForRowAt:
cell.contentView.backgroundColor = UIColor.clear
It's work for me.
The two first answer doesn't work for me so I add a clear background everywhere and the white bg go away.
cell.layer.backgroundColor = UIColor.clear.cgColor
cell.backgroundColor = .clear
tableView.layer.backgroundColor = UIColor.clear.cgColor
tableView.backgroundColor = .clear
SWIFT 5.5.3
You have to set both table and cell background clear.
Inside tableView function:
tableView.backgroundColor = .clear
cell.backgroundColor = .clear
tableView.tableFooterView = UIView()
For this to work you have to implement a new tableview method:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell,
forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.clear
}
You are missing setting the background colour of contentView of the cell.
cell.contentView.backgroundColor = UIColor.clear
Maybe, you can check your table view's alpha.
Setting these wont be enough as you have not applied clear color to the contentView of the tableView and so it is coloring white in it. Try this in cellForRowAtIndexPath
cell.contentView.backgroundColor = UIColor .clearColor()
Make sure in the Identity Inspector you have Custom Class -> Class "YourCustomClassCellName" selected in the Drop Down Menu. With your UITableViewCell selected in Interface Builder, make sure your Identifier is "YourCellName" and then in the func tableView(_ tableViewL UITableView, cellForRowAt) method before the return cell that you have cell.backgroundColor = UIColor.clear i.e,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellName", for: indexPath) as? YourCellNameClass {
cell.configureCell(parameter: ClassDataModel[indexPath.row])
cell.backgroundColor = UIColor.clear
return cell
} else {
return UITableViewCell()
}
I had the same issue and wrote all the code before I checked to see make sure my CellClass was selected and face palmed when I spent an hour wondering why it worked on another viewController and not in this instance. Even if you use IB to clear out the default white backgrounds, you still have to write the cell.backgroundColor = UIColor.clear. I was like yep, I found another bug in 8.2.1, but no just an iD10t operator bug.
I tested using swift 3. It's working
uiTableName.backgroundView = nil
uiTableName.backgroundColor = UIColor.clear
In the Storyboard for the TableView and TableViewCell, instead of setting the background to "Clear/Default" set the background to white (or any color) with an opacity of 0.
Also make sure you set the ContentView background to clear, but the TableViewCell is what was tripping me up. By doing this you shouldn't need to do anything in the code.
In Swift 4, Write the following code under viewDidLoad of your desired class:-
tableView.backgroundColor = UIColor.clear
Write the following code under the cellForRowAt i.e dataSource of your tableView:-
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifierName", for: indexPath) as! UITableViewCell //Cell ClassName
cell.layer.backgroundColor = UIColor.clear.cgColor
return cell
Why nobody said about this?:
#IBOutlet weak var tableView: UITableView!
// Drag and drop the tableView from the storyboard
override func viewDidLoad() {
super.viewDidLoad()
tableView.isHidden = true
}
The result gives you an opportunity to hide the whole tableView with a single line.
Related
everyone!
My problem sound like this:
I want to make my custom cell rounded in my app in iPhone. But when I try to do something like this:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? MainTableViewCell
else { fatalError("DequeueReusableCell failed while casting") }
cell.imageView?.layer.cornerRadius = cell.frame.size.height / 2
cell.imageView?.clipsToBounds = true
cell.imageView?.contentMode = .scaleAspectFill
cell.textLabel?.textColor = #colorLiteral(red: 0.03921568627, green: 0.3969546359, blue: 1, alpha: 1)
cell.textLabel?.font = UIFont(name: "EuphemiaUCAS", size: 22)
cell.textLabel?.text = restaurantNames[indexPath.row]
cell.textLabel?.numberOfLines = 0
cell.imageView?.image = UIImage(named: restaurantNames[indexPath.row])
return cell
}
// MARK: - Table View Delegate
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 85
}
I have a strange result: Result #1
But when I change this line:
cell.imageView?.layer.cornerRadius = cell.frame.size.height / 2
To this:
cell.imageView?.layer.cornerRadius = 85 / 2
As you can see, everything is in order:
Result #2
But this is not the right way to solve the problem
Thanks for all answers!
Add it in your cell class instead of cellForRow
class MainTableViewCell: UITableViewCell {
override func layoutSubviews() {
imageOfPlace.layer.cornerRadius = imageOfPlace.bounds.midY // or imageOfPlace.bounds.height/2
imageOfPlace.clipsToBounds = true
}
}
The problem has come because the cellForRow AtIndexPath called before heightForRowAt indexPath called. It's a reason why the cell at that time has default height size, and you used it to calculate cornerRadius.
You can use your way to fix the problem, or you use the image to look like a mask to put in above the image in the cell. This way can help you use less energy for calculating cornerRadius when the delegate is called.
UITableView provides default sizes for rows when you dequeue cells. That's why cell.frame.size.height/2 is not 85/2 rather than detaultHeight / 2. TableView cell height later becomes fix when heightForRowAt delegate method called. You can find more about this Here.
Change the corner radius property of imageView in awakeFromNib method under MainTableViewCell.
class MainTableViewCell: UITableViewCell {
override func awakeFromNib() {
imageView.layer.cornerRadius = bounds.height/2
}
}
My requirement is to highlight the contentView of the selected cell. As of now the issue is previously selected cell (contentView) is also get highlighted. Code as bellow
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.viewWithTag(20)?.layer.borderWidth = 2.0
cell?.viewWithTag(20)?.layer.borderColor = UIColor.blue.cgColor
}
Read up cell reuse. You need to reset your cell's layer in prepareForReuse.
Follow these 3 steps:
Go to storyboard and select your cell.
In the right panel in Attributes Inspector, change Selection Style to Default.
Also select your tableview and in project Attributes, change Selection to Single Selection
The issue is cell reuse; you need to reset to original state of the cell like so:
class HelightTableViewCell: UITableViewCell {
override func prepareForReuse() {
super.prepareForReuse()
self.layer.borderWidth = 0.0
self.layer.borderColor = UIColor.clear.cgColor
}
}
While creating the cell on prepareForReuse change to original state and on did select set the properties like so:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) as? HelightTableViewCell {
return
}
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.blue.cgColor
}
I am working with a tableview in editing mode. I am using the checkmark multi select method as you can see in the iOS built in mail edit mode -- also linked here.
My issue is that when a cell is selected, the background changes to the default tintColor.
My expected outcome is that the tableViewCell onSelect fills in the checkmark but does not change the background color.
I have tried changing the selectionStyle to .none -- this makes it so I cannot select the cell at all in editing mode. I have also tried changing the selected background view without success.
open override func viewWillLoad(withData data: Any!) {
self.view.backgroundColor = .gray
self.tableView = UITableView()
self.tableView.setEditing(true, animated: true)
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.allowsMultipleSelection = true
self.tableView.allowsSelectionDuringEditing = true
self.view.addSubview(tableView)
}
public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.init(rawValue: 3)!
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// dequeueing my cell here
cell.textLabel?.text = data[indexPath.row]
// cell.selectionStyle = ????
return cell
}
Is there any way to achieve this other than creating a custom button?
It turns out that there is a specific background for tableViews that use Multiple Selection!
My solution was to use the following code on my cells:
let selectedView = UIView()
selectedView.backgroundColor = cellBackgroundColor
self.multipleSelectionBackgroundView = selectedView
self.selectionStyle = .default
If you are only using single selection you can use the following:
let selectedView = UIView()
selectedView.backgroundColor = cellBackgroundColor
self.selectedBackgroundView = selectedView
self.selectionStyle = .default
You can remove that highlighted color in storyboard.
select selection to None
And also you can remove that color by code
cell.selectionStyle = .none
Try this:
func tableView(_ tableView: UITableView,
shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return !tableView.isEditing
}
I seem to have a weird problem with one of my tableviews where I can't hide the separator lines ( I think they're separator lines, except that they're centred on the screen instead of hard up against the right hand side ).
I create everything programmatically so no storyboard issues here.
You can see the tiny 1px line above each of the cells below.
I load my table using:
self.tableView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.backgroundColor = UIColor.whiteColor()
self.tableView.scrollEnabled = true
self.tableView.bounces = false
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
self.view.addSubview(self.tableView)
I also have a custom header which I implement using the following code:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header:UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.textLabel.textColor = UIColor.whiteColor()
header.textLabel.frame = header.bounds
header.textLabel.textAlignment = NSTextAlignment.Center
view.tintColor = constants.getTintColor()
header.textLabel.textColor = UIColor.whiteColor()
}
I've tried loading the table without the willDisplayHeaderView and the issue persists.
I have also tried adding
tableview.separatorStyle = UITableViewCellSeparatorStyle.None
and
tableView.separatorColor = UIColor.clearColor()
to the following methods:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
return self.boards.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
return self.boards[section].items.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
return self.boards[section].name
}
EDIT:
The cells are standard UITableViewCells with alternating colors for the cells, this is being set through:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
if indexPath.row % 2 == 0 {
// Even
cell.backgroundColor = constants.UIColorFromRGB(0x1EACE0)
} else {
// Odd
cell.backgroundColor = constants.UIColorFromRGB(0x6FBEE5)
}
cell.textLabel?.textColor = UIColor.whiteColor()
cell.textLabel?.text = self.boards[indexPath.section].items[indexPath.row].title
return cell
}
EDIT 2:
I've now added separator lines in and these aren't separators because you can still see them below the separator line.
HELP! I'm confused. I have a number of other tableviews setup the same (as far as I can see) and they work fine.
Thanks SO!
You can either use the Table View property for the seperatorStyle and use the property as such UITableViewCellSeparatorStyleNone
or use remove the separator from the StoryBoard
Set the property of the Seperator to None
Or one thing more while you are using header and Footer so the separator line would be Zero but there might be a extra separator or header and footer so refer to this link
Eliminate extra separators below UITableView
in your viewDidLoad() function, add this:
tableView.tableFooterView = UIView()
Since you are not using custom cell's you need to do the following.
Create cell's as let cell : UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell")
and remove self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
Here's a little additional information about eliminating the spurious separators that might be helpful. The solution that the originator said worked was this one that creates a new cell:
let cell : UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell")
But the originator did not say how he worked this into his code, whose original version used the two argument version of dequeueReusableCellWithIdentifier:
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
I experienced the same problem of spurious separators and found that using the single argument version of dequeueReusableCellWithIdentifier also fixes the problem:
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
I ran into same issue recently, and finally fixed by this in cell's init:
self.contentView.backgroundColor = UIColor.AppBgColor;
self.backgroundColor = UIColor.AppBgColor;
If you look closely in View Hierarchy, the cell's background and it's contentView's background are not same, which leads to this issue.
I have a regular UITableView with single selection enabled. My problem is that if the user selects multiple rows then the original rows remain selected. I also have a problem where the highlight remains gray no matter if I set the cell.selectionStyle = UITableViewCellSelectionStyle.Blue
My view controller is defined in the Storyboard.
Table View
Content: Dynamic Prototypes
Selection: Single Selection
Show Selection on Touch [X]
Background: Black Color
Index Row Limit: 0
Table Cell View
Style: Custom
Selection: Blue
Background: Black Color
Here are some screenshots:
Here is my code:
class AreaViewController: UITableViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
self.tableView.backgroundColor = backgroundColour
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("areacell", forIndexPath: indexPath) as! UITableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.Blue
cell.textLabel?.text = "Cell Contents"
cell.textLabel?.textColor = UIColor.whiteColor()
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let cell = tableView.dequeueReusableCellWithIdentifier("areacell", forIndexPath: indexPath) as! UITableViewCell
}
}
I must be missing something obvious but I've not been able to see anything non standard.
From the UITableViewCell Class Reference
UITableViewCellSelectionStyleBlue The cell has a default background
color when selected.
In iOS 7, the selection color is no longer blue. Use
UITableViewCellSelectionStyleDefault instead.
If you want a special background color for selected cells you have to set the cells' backgroundView:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.redColor()
cell.selectedBackgroundView = backgroundView
return cell
}
Looks like this:
Argh! I found it at last. Seems like I was calling let cell = tableView.dequeueReusableCellWithIdentifier("areacell", forIndexPath: indexPath) as! UITableViewCell in the didSelectRowAtIndexPath method. Removing it caused everything to start working again. Obvious really. Thanks for your help zisoft in putting me on the right road.