【问题标题】:UITableView clear backgroundUITableView 清除背景
【发布时间】:2013-09-16 04:46:44
【问题描述】:

我意识到 iOS 7 还没有正式发布,我们不应该讨论它,但我要疯狂地试图找出这个问题。在 iOS 6 上,我的表格视图是透明的并且看起来很棒。第一次运行iOS 7,背景是白色的。

我尝试将表格背景颜色、单元格颜色等设置为 UIColor clearColor 但没有任何变化。

如何解决这个问题?

【问题讨论】:

  • 尝试将表格的backgroundView 设置为清晰的?
  • 能否显示设置背景颜色的代码?父视图呢?也许表格视图背景清晰但父级不清晰?
  • 我注意到了同样的事情,但是当我安装了比我当前版本早于 iOS 7.0 的同一个应用程序时,它并没有发生。那么这个问题是出在xCode5.0上吗?

标签: ios uitableview


【解决方案1】:
    // Fix for iOS 7 to clear backgroundColor
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView = [[UIView new] autorelease];
    cell.selectedBackgroundView = [[UIView new] autorelease];

在 cellForRowAtIndexPath 中

另外,请确保您的 tableview 实际上具有透明背景(在情节提要中):

【讨论】:

  • George,在方法中添加该代码 - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  • 对于这种情况,如果确实需要selectedBackgroundView,可以在cellForRowAtIndexPath中放入如下内容: cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourBgImage.png"]]自动释放];甚至在 awakeFromNib: self.selectedBackgroundView = ... 注意,即:self.backgroundColor = [UIColor clearColor];在 iOS7 下的 awakeFromNib 中不起作用。但是:cell.backgroundColor = [UIColor clearColor];仅在 iOS 7.0 的 cellForRowAtIndexPath 中正常工作
  • 只需要添加行:cell.backgroundColor = [UIColor clearColor];
  • 这就像魅力 cell.backgroundColor = [UIColor clearColor];
  • 如何在 Swift 中做到这一点?
【解决方案2】:

这样写:

cell.backgroundColor = [UIColor clearColor];

在本节中:

cellForRowAtIndexPath

【讨论】:

  • 在 iOS 6 中,将 backgroundView 设置为空视图就足够了,但 iOS 7 需要此附加代码。
【解决方案3】:

先尝试将 backgroundView 设置为 nil。

[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor clearColor]];

不确定这是否是 iOS7 文档的更改,还是一直存在,只是不影响背景颜色,但根据 UITableView Class Reference @property backgroundView

“您必须将此属性设置为 nil 才能设置表格视图的背景颜色。”

编辑:更正的代码语法

【讨论】:

    【解决方案4】:

    已经回答了这个问题,但在很多方面都不正确。

    您需要实现以下委托方法:

        - (void)tableView:(UITableView *)tableView  willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [cell setBackgroundColor:[UIColor clearColor]];
    }
    

    您不能将修复程序放在 cellForRowAtIndexPath 中,因为这是在渲染单元格之后,它会在背景设置为清除之前闪烁白色背景(在较慢的设备上)。

    使用这个委托方法,你的问题就解决了!

    【讨论】:

    • 是的。对于 iOS 7,这是清除背景的方法。以上所有其他建议可能都不起作用。
    【解决方案5】:

    根据文档(UITableViewCell Class Reference),实际上更改单元格背景颜色的官方正确位置不同:

    无论您使用预定义的还是自定义的单元格,您都可以更改单元格的 背景使用 backgroundView 属性或通过更改 继承了 backgroundColor 属性。在 iOS 7 中,单元格有一个白色 默认为背景;在早期版本的 iOS 中,单元格继承 封闭表视图的背景颜色。如果你想改变 单元格的背景颜色,在 tableView:willDisplayCell:forRowAtIndexPath: 表格视图的方法 委托。

    【讨论】:

    • 我正在尝试这个,但显示的第一个单元格都有白色背景。如果我通过滚动创建新单元格,那么它们将具有我想要的透明背景。不知道还能尝试什么……
    • 没关系,这似乎是一个时间问题。我在 awakeFromNib 做事,太早了。
    【解决方案6】:

    swift 3、4 和 5

    cell.backgroundColor = UIColor.clear
    

    【讨论】:

      【解决方案7】:

      这是一个非常令人沮丧的问题。这是我目前的解决方案:

      将此添加到您的 UITableViewCell 子类中。

      - (void)didMoveToSuperview {
          [super didMoveToSuperview];
      
          self.backgroundColor = [UIColor clearColor];
      }
      

      【讨论】:

      • 天哪。谢谢你。拯救了我的夜晚。你的答案应该是被接受的。
      【解决方案8】:

      这在 iOS7+ 中对我有用:

      self.tableView.backgroundColor =[UIColor blueColor];
      self.tableView.opaque = NO;
      self.tableView.backgroundView = nil;`
      

      然后在cellForRowAtIndexPath:

      cell.backgroundColor = [UIColor clearColor];
      

      【讨论】:

        【解决方案9】:

        试试这个代码sn-p

        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
        

        【讨论】:

          【解决方案10】:

          这仅在我为每个单元格编辑清晰的背景颜色和为表格本身编辑清晰的颜色时才对我有用.. 都以编程方式进行

          为表格设置清晰的颜色:

          override func viewDidLoad() {
              super.viewDidLoad()
              // Do any additional setup after loading the view, typically from a nib.
              initMenu()
              myTableView.backgroundColor = UIColor.clearColor()
          }
          

          设置单元格的颜色:

          func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCellWithIdentifier("tablecellid", forIndexPath: indexPath)
              cell.backgroundColor = UIColor.clearColor()
              return cell
          }
          

          【讨论】:

            【解决方案11】:

            一件好事。 UITable的默认颜色好像是白色的(不知道为什么)

            但最好改变它。

            【讨论】:

            • 完美!那是失去的和平。无需额外的代码行!
            【解决方案12】:

            第一组

            tableView.backgroundColor = [UIColor clearColor];
            

            第二组

            tableCell.backgroundColor = [UIColor clearColor];
            

            【讨论】:

              【解决方案13】:

              为表格视图创建 IB Outlet @IBOutlet weak var yourTable : UITableView!

              在视图中加载

              override func viewDidLoad() {
                  yourTable.delegate = self
                  yourTable.dataSource = self
              
                  yourTable.backgroundColor = UIColor.clearColor()
              }
              

              如果你想清除单元格的颜色,也可以在

              func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
              
                  cell.backgroundColor = UIColor.clearColor() 
              
              }
              

              【讨论】:

                【解决方案14】:

                在我的应用程序中,当我更新 iOS 7 时,我必须将 UITableViewCell 类上的 backgroundColor 设置为 [UIColor clearColor] 颜色。

                【讨论】:

                  【解决方案15】:

                  试试

                  [myTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
                  [myTable setSeparatorInset:UIEdgeInsetsZero];
                  

                  cell.backgroundColor = [UIColor clearColor];
                  

                  【讨论】:

                    【解决方案16】:

                    在我的例子中,单元格是使用 xib 创建的。似乎 xcode5 上的界面生成器无法将 clearColor 设置为 cell.backgroundColor。

                    我需要做的只是设置

                    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                    {
                    // get the cell from the nib
                    //then force the backgroundColor
                    cell.backgroundColor = [UIColor clearColor]
                    return cell;
                    }
                    

                    【讨论】:

                    • 控件到达非空函数的末尾
                    【解决方案17】:

                    只需为表格和单元格选择“视图背景上的清除颜色”即可。

                    【讨论】:

                      【解决方案18】:

                      斯威夫特 3

                      override func viewDidLoad() {
                              super.viewDidLoad()
                              tableView.backgroundColor = UIColor.clear
                      }
                      

                      【讨论】:

                        【解决方案19】:
                        // Fix iOS 7 clear backgroundColor compatibility 
                        // I Think this two lines only are enough
                        
                        cell.backgroundColor = [UIColor clearColor];
                        cell.selectedBackgroundView = [[UIView new] autorelease];
                        

                        【讨论】:

                          【解决方案20】:

                          设置

                          tableView.backgroundColor = [UIColor clearColor];
                          

                          在 viewDidLoad 中。

                          如果这不起作用,请尝试:

                          tableView.backgroundView = nil;
                          

                          【讨论】:

                            【解决方案21】:

                            在 Swift 3 中

                            override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                                let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath)
                                cell.backgroundColor = .clear
                                cell.backgroundView = UIView()
                                cell.selectedBackgroundView = UIView()
                                return cell
                            }
                            

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 1970-01-01
                              • 2012-06-06
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 2023-03-22
                              相关资源
                              最近更新 更多