[英]How do I make this UITableView clear (transparent) in Swift 3
我如何在 Swift 3 中使這個UITableView
和它的單元格清晰。
我已經瀏覽了以前的線程,但我仍然得到白色背景。
正如您從我的代碼中看到的,我嘗試了提到的各種方法:
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.
}
在我的單元格表功能中:
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
}
此 gif 顯示了問題 - 請注意顯示表格的黑線只是未填充(因為沒有人登錄)
但有一秒鍾它是清楚的,然后變成白色。 我哪里錯了?
這是我發現的另一篇文章:
蘋果文件說
... 在 iOS 7 中,單元格默認為白色背景; 在早期版本的 iOS 中,單元格繼承了封閉表格視圖的背景顏色。 如果要更改單元格的背景顏色,請在表視圖委托的 tableView:willDisplayCell:forRowAtIndexPath: 方法中執行此操作。
您可能需要使用 willDisplayCell UITableView 委托方法為您的表格視圖設置透明背景。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
我如何應用上面的代碼,因為它適用於 iOS 7?
注意:以下代碼在Swift 3
進行了測試。
方法一:
從storyboard
選擇您的 tableViewCell 並轉到View
下的Attributes Inspector
更改Background
以clear
方法 2:在cellForRowAt
嘗試以下代碼
cell.layer.backgroundColor = UIColor.clear.cgColor
注意:如果上述方法不起作用。嘗試按shift + option + command + k
清除您的項目構建
更新:從下面的代碼更新你的cellForRowAt
...
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
}
你可以試試這個
在viewDidLoad 中:
tableView.backgroundColor = UIColor.clear
在cellForRowAt 中:
cell.contentView.backgroundColor = UIColor.clear
這對我有用。
您必須將表格和單元格背景都設置為清晰。
在 tableView 函數中:
tableView.backgroundColor = .clear
cell.backgroundColor = .clear
tableView.tableFooterView = UIView()
前兩個答案對我不起作用,所以我在各處添加了清晰的背景,白色背景消失了。
cell.layer.backgroundColor = UIColor.clear.cgColor
cell.backgroundColor = .clear
tableView.layer.backgroundColor = UIColor.clear.cgColor
tableView.backgroundColor = .clear
斯威夫特 5.1.2
為此,您必須實現一個新的 tableview 方法:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell,
forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.clear
}
您缺少設置單元格 contentView 的背景顏色。
cell.contentView.backgroundColor = UIColor.clear
也許,您可以檢查表視圖的 alpha。
設置這些是不夠的,因為您沒有對tableView
的contentView
應用clear color
,因此它在其中着色為白色。 在 cellForRowAtIndexPath 中試試這個
cell.contentView.backgroundColor = UIColor .clearColor()
確保在身份檢查器中,您在下拉菜單中選擇了自定義類 -> 類“YourCustomClassCellName”。 在界面生成器中選擇 UITableViewCell 后,確保您的標識符是“YourCellName”,然后在返回單元格之前的 func tableView(_ tableViewL UITableView, cellForRowAt) 方法中,您有 cell.backgroundColor = UIColor.clear 即,
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()
}
當我花了一個小時想知道為什么它在另一個 viewController 上工作而不是在這個實例中時,我遇到了同樣的問題並在檢查以確保我的 CellClass 被選中並面對手掌之前編寫了所有代碼。 即使你用IB清除了默認的白色背景,你還是要寫cell.backgroundColor = UIColor.clear。 我就像是的,我在 8.2.1 中發現了另一個錯誤,但不僅僅是 iD10t 操作員錯誤。
我使用 swift 3 進行了測試。它正在工作
uiTableName.backgroundView = nil
uiTableName.backgroundColor = UIColor.clear
在 Swift 4 中,在所需類的 viewDidLoad 下編寫以下代碼:-
tableView.backgroundColor = UIColor.clear
在你的tableView的cellForRowAt即dataSource下寫下以下代碼:-
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifierName", for: indexPath) as! UITableViewCell //Cell ClassName
cell.layer.backgroundColor = UIColor.clear.cgColor
return cell
為什么沒人說這個?:
@IBOutlet weak var tableView: UITableView!
// Drag and drop the tableView from the storyboard
override func viewDidLoad() {
super.viewDidLoad()
tableView.isHidden = true
}
結果讓您有機會用一行隱藏整個 tableView。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.