Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make this UITableView clear (transparent) in Swift 3

Tags:

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?

like image 861
RDowns Avatar asked Nov 18 '16 13:11

RDowns


People also ask

How do you make a view transparent in Xcode?

xib, you simply do it in interface builder by selecting the option "Clear Color" for the Background of the view in the Utilities Pane (the pane on the right). "Clear Color" will give the view a completely transparent background.

How do I make a transparent view in SwiftUI?

Any SwiftUI view can be partially or wholly transparent using the opacity() modifier. This accepts a value between 0 (completely invisible) and 1 (fully opaque), just like the alpha property of UIView in UIKit.


1 Answers

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

enter image description here

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
}
like image 113
Joe Avatar answered Oct 11 '22 10:10

Joe