ios - Swift 4 backgroundColor 错误?

标签 ios swift uitableview

我的 View 透明度有问题,尤其是 UITableView

只是为了确定。我已经有 3 年的 swift 经验,这是我第一次遇到这个问题。

所以在 apple 的文档中它说了以下内容: 背景颜色 View 的背景颜色。

此属性的更改可以动画化。默认值为 nil,这会产生透明的背景颜色。”

但是当对 backgroundColortableView.backgroundColor = UIColor.clear 应用清晰颜色时,背景仍然显示为白色。

这是错误还是功能?

编辑:

tableView 不再是问题。它是 tableView 中的单元格。 已经尝试过的解决方案正在清除所有 subview 的背景颜色。什么都没有改变。

编辑 29.09.17

好的,我调试了我的应用程序并得到了这个:

UICachedDeviceWhiteColor 和 cachedColor。

UICachedDeviceWhiteColor

谷歌搜索也没有帮助。

编辑 29.09.2017 当天晚些时候......

问题发生在从 swift 3 更新到 swift 4 时。 我用 swift 4 做了一个新项目,但它实际上工作正常。 因为这是我必须解决的问题。我会尝试一些事情并不时更新这篇文章。

最佳答案

在 UITableViewCell 和 UICollectionViewCell 中有一个名为 contentView 的属性 View 。

一个常见的错误是直接操作单元格。而是尝试操纵单元格内容 View :

Obj-c

UITableViewCell *cell = ....// get the cell

cell.backgroundColor = ... // BAD !

cell.contentView.backgroundColor = [UIColor redColor] // this is the correct way.

swift

let cell = ...
cell.contentView.backgroundColor = UIColor.red // make sure its the contentView 

还有一些颜色和不透明度的细微差别:

如果您希望背景具有不透明度(即小于 100%),请确保您改变 View 的不透明度(即view. alpha = 0.5 BAD) - 改为更改颜色的不透明度值:

cell.contentView.backgroundColor = [UIColor colorWithRed:... green:... blue:... alpha:0.5];

另一个有用的技巧是暂停应用程序并按下“可视化调试”按钮。

搜索您的 View 并在检查器中检查它的属性

关于ios - Swift 4 backgroundColor 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46471447/

相关文章:

ios - 当设备在 3g 网络上时 AFNetworking 不工作,但当应用程序在 wifi 网络上时它工作

iOS 11 : Add other nav items next to a search bar embedded in navigation item title view

ios - 调用 setWorldOrigin 时子节点不可见?

ios - 如何处理自定义 Tableview 单元格 iOS 中的“收藏夹”按钮点击?

ios - 如何垂直对齐(居中)UITableView 的内容?

ios - 水平 StackView 的正确对齐和分布选择

ios - 在Xcode中更改导航栏高度后如何更改标题的位置

ios - React-native-maps lite 模式不适用于 Ios 模拟器

ios - 在 iOS 应用程序中管理订阅的链接最终会出现空白屏幕

ios - 在单击不同的 TableView 单元之前,TableView 单元不加载 View Controller