简体   繁体   English

带有iOS7的iPad上具有透明背景的UITableView / UITableViewCell挑战

[英]UITableView / UITableViewCell challenge with transparent background on iPad with iOS7

Last night I decided to upgrade to Xcode 5 and take a look at my current project. 昨晚我决定升级到Xcode 5,并查看我当前的项目。 After updating my storyboards to the new UI everything looked great and ran fine. 将情节提要更新到新的UI后,一切看起来都很好并且运行良好。 Since I have a universal binary, I decided to test things on iPad as well and noticed that a new white background had been introduced into my UITableview where there once used to be a transparent / clear color. 由于我具有通用二进制文件,因此我决定也要在iPad上进行测试,并注意到UITableview中引入了新的白色背景,该背景曾经曾经是透明/透明的颜色。 This appears to be happening on the cell level, not the table level. 这似乎发生在单元级别,而不是表级别。 When I run things on the 6.1 simulator everything looks fine on iPad & iPhone. 当我在6.1仿真器上运行时,在iPad和iPhone上一切正常。 And everything looks fine on iPhone for iOS7. 在iOS7的iPhone上,一切看起来都不错。

Everything that I have set up for interface builder is identical for iPhone & iPad. 我为界面生成器设置的所有内容对于iPhone和iPad都是相同的。 From what I can tell it has something to do with this new "content view" (which is a subgroup of the Item Cell) not honoring a transparent value / setting. 据我所知,它与这个新的“内容视图”(它是Item单元的一个子组)有关,不符合透明值/设置。

Any thoughts / ideas? 有什么想法/想法吗?

After wasting multiple hours with interface builder, I'm thinking that there might be a bug there. 在使用界面生成器浪费了多个小时之后,我认为那里可能存在错误。 So I started to look for a programatic answer. 因此,我开始寻找程序化的答案。 Apparently had I started here I could have saved a ton of time. 显然,如果我从这里开始,我可以节省很多时间。 By adding to the method: 通过添加方法:

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

I was able to solve the transparency issue on iPad by adding this one line: 通过添加以下这一行,我能够解决iPad上的透明性问题:

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

Hope this helps everyone else with the white background seen for ipad with tables and iOS7! 希望这对使用表格和iOS7的ipad具有白色背景的其他人有所帮助!

If you are using a custom UITableViewCell and invoking it from storyboard/xib you can use the following code. 如果使用自定义UITableViewCell并从情节提要/ xib调用它,则可以使用以下代码。

@implementation YourCustomTableViewCell

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

In case anyone else is still having trouble with table view/cell transparency on iPad, this may help (copied from https://stackoverflow.com/a/31396483/2301213 , it's in swift since the times they are a changin') 万一其他人仍无法在iPad上查看表格视图/单元格透明度,这可能会有所帮助(复制自https://stackoverflow.com/a/31396483/2301213 ,因为它们一直在变化,所以它很快出现了)

It seems that somewhere in the process of adding a UITableView to the window (between willMoveToWindow and didMoveToWindow), some iPad's reset the backgroundColor of the table view to white. 似乎在向窗口添加UITableView的过程中(在willMoveToWindow和didMoveToWindow之间),某些iPad将表视图的backgroundColor重置为白色。 It does this covertly without using the backgroundColor property. 它秘密地执行此操作,而不使用backgroundColor属性。

I now use this as a base class in place of UITableView when I need a colored/transparent table... 现在,当我需要彩色/透明表时,可以将其用作UITableView的基类。

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

Cells also have their backgroundColor's set to white on my iPad in the same way (ie those that are in the table during the move to the window), so the same applies to them, lest you end up with the odd opaque cell popping up from time to time as it is reused ... 单元格也以相同的方式在iPad上将它们的backgroundColor设置为白色(即,移动到窗口期间在桌子上的那些),因此对它们也是如此,以免最终导致从屏幕上弹出奇数不透明的单元格不时地重复使用...

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

If you are using a static cell table. 如果您使用的是静态单元格表​​。 you can do the following: 您可以执行以下操作:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

     cell.backgroundColor = UIColor.clearColor()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM