【问题标题】:UITableView / UITableViewCell challenge with transparent background on iPad with iOS7带有iOS7的iPad上具有透明背景的UITableView / UITableViewCell挑战
【发布时间】:2013-09-24 22:25:17
【问题描述】:

昨晚我决定升级到 Xcode 5 并查看我当前的项目。将我的故事板更新到新的 UI 后,一切看起来都很棒并且运行良好。由于我有一个通用二进制文件,我决定在 iPad 上也进行测试,并注意到我的 UITableview 中引入了一个新的白色背景,那里曾经是透明/清晰的颜色。这似乎发生在单元格级别,而不是表级别。当我在 6.1 模拟器上运行时,iPad 和 iPhone 上的一切看起来都很好。在 iOS7 的 iPhone 上一切看起来都很好。

我为界面生成器设置的所有内容都与 iPhone 和 iPad 相同。据我所知,它与这个不尊重透明值/设置的新“内容视图”(它是项目单元格的子组)有关。

有什么想法/想法吗?

【问题讨论】:

    标签: objective-c ipad uitableview ios7


    【解决方案1】:

    在使用界面构建器浪费了多个小时之后,我认为那里可能存在错误。所以我开始寻找一个程序化的答案。显然,如果我从这里开始,我可以节省大量时间。通过添加到方法中:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    我可以通过添加这一行来解决 iPad 上的透明度问题:

    cell.backgroundColor = [UIColor clearColor];  // Adding this fixes the issue for iPad
    

    希望这可以帮助其他所有使用 ipad 和 iOS7 的白色背景的人!

    【讨论】:

    • 这也是我唯一的解决方案。谢谢! (我试图保持冷静,并将我的所有单元格和内容视图背景设置为在 XIB 中清除,但这仅在首次加载时有效(reloadTable 将在没有您的编程解决方案的情况下设置白色背景))
    • 绝对是 Xcode 5 的错误。我必须写 cell.backgroundColor = [UIColor clearColor];作为方法中的最后一条指令(cellForRowAtIndexPath),因为如果在单元格中的指令不起作用之前更改其他内容。
    • 同样的问题,这个确切的解决方案也为我解决了,谢谢。
    • 看看 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    • 这在 Xcode 6.0.1 下仍然是一个问题。这个解决方案工作得很好:)
    【解决方案2】:

    如果您使用自定义 UITableViewCell 并从 storyboard/xib 调用它,您可以使用以下代码。

    @implementation YourCustomTableViewCell
    
    - (void) awakeFromNib
    {
        self.backgroundColor = [UIColor clearColor];
    }
    

    【讨论】:

      【解决方案3】:

      如果您使用的是静态单元格表​​。您可以执行以下操作:

      override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
      
           cell.backgroundColor = UIColor.clearColor()
      }
      

      【讨论】:

        【解决方案4】:

        如果其他人仍然在 iPad 上的表格视图/单元格透明度方面遇到问题,这可能会有所帮助(从 https://stackoverflow.com/a/31396483/2301213 复制,自从他们改变时代以来它很快)

        似乎在向窗口添加 UITableView 的过程中(在 willMoveToWindow 和 didMoveToWindow 之间),某些 iPad 将 table view 的 backgroundColor 重置为白色。它在不使用 backgroundColor 属性的情况下秘密地执行此操作。

        当我需要彩色/透明表格时,我现在使用它作为基类来代替 UITableView...

        class ColorableTableView : UITableView {
            var _backgroundColor:UIColor?
            override var backgroundColor:UIColor? {
                didSet {
                    _backgroundColor = backgroundColor
                }
            }
            override func didMoveToWindow() {
                backgroundColor = _backgroundColor
                super.didMoveToWindow()
            }
        }
        

        单元格的背景颜色在我的 iPad 上也以相同的方式设置为白色(即在移动到窗口期间位于表格中的单元格),因此同样适用于它们,以免您最终得到奇怪的不透明单元格重复使用时不时弹出...

        class ColorableTableViewCell : UITableViewCell {
            var _backgroundColor:UIColor?
            override var backgroundColor:UIColor? {
                didSet {
                    _backgroundColor = backgroundColor
                }
            }
            override func didMoveToWindow() {
                backgroundColor = _backgroundColor
                super.didMoveToWindow()
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2010-11-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-28
          • 2018-05-31
          • 2011-07-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多