向子视图添加约束使背景颜色不显示
所以我使用自定义函数来格式化我添加到UICollectionViewCell
的子视图。这是来自Brian Voong的公共项目:https://github.com/purelyswift/facebook_feed_dynamic_cell_content/blob/master/facebookfeed2/ViewController.swift。向子视图添加约束使背景颜色不显示
func addConstraintsWithFormat(format: String, views: UIView...) {
var viewsDictionary = [String: UIView]()
for (index, view) in views.enumerate() {
let key = "v\(index)"
viewsDictionary[key] = view
view.translatesAutoresizingMaskIntoConstraints = false
}
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
}
有趣的是,在我的UICollectionView
我添加了一个SubView
单个单元格,并设置背景颜色为白色。注释掉为子视图设置背景的线条时,背景为白色,在取消注释设置子视图的可视格式约束的线条时,不会设置背景色。
下面是两行,其揍对方:
func chronicleOneClicked(sender: UIButton) {
point1view.backgroundColor = UIColor.whiteColor()
addSubview(point1view)
//When the below is commented the background of point1view disappears
//addConstraintsWithFormat("|-50-[v0]-50-|", views: point1view)
}
当我做print(subviews)
我看到UIView
与白色的背景颜色是在视图中堆叠(堆叠的顶部)的最高水平。当我打印出subviews[subviews.count-1].backgroundColor
时,我得到了我期望的Optional(UIDeviceWhiteColorSpace 1 1)
。这是奇怪的,因为颜色不显示。
我不知道如何看看幕后发生的事情,以确认在后一种情况下设置背景。
这一切都发生在了UiCollectionViewCell
一类我使用的类可以在其全部在这里可以查看我的UICollectionView
细胞中的一种:
https://gist.github.com/ebbnormal/edb79a15dab4797946e0d1f6905c2dd0
下面是一个屏幕截图在这两种情况下,第一种情况是行addConstraintsWithFormat
被注释掉,第二种情况是未注释的情况:point1subview
的子视图在第一种情况下以白色背景突出显示。
这是我如何设置的意见。这一切都发生在重写UICollectionViewCell
class myClass : UICollectionViewCell {
var chronicle: BrowsableChronicle? {
didSet{
//etc.
point1.addTarget(self, action: #selector(chronicleOneClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
let point1 : PointButtonView = {
let pointView = PointButtonView(frame: CGRectMake(0, 0, 25, 25))
return pointView
}()
//NOTE here is where I create the view, whose background doesn't display
let point1view : UIView = {
let pointView = UIView(frame: CGRectMake(0, 0, 200, 270))
pointView.backgroundColor = UIColor.whiteColor()
let title = UILabel(frame: CGRectMake(0, 0, 200, 21))
title.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0)
pointView.addSubview(title)
let summary = UILabel(frame: CGRectMake(0, 0, 190, 260))
summary.lineBreakMode = NSLineBreakMode.ByWordWrapping
summary.numberOfLines = 4
summary.font = UIFont(name:"HelveticaNeue", size: 12.5)
pointView.addSubview(summary)
let button = UIButton(frame: CGRectMake(0, 200, 190, 30))
button.backgroundColor = UIColor(red:0.00, green:0.90, blue:0.93, alpha:1.0)
pointView.addSubview(button)
pointView.tag = 100
return pointView
}()
//NOTE: here is where I add the subview to the UICollectionViewCell view
func chronicleOneClicked(sender: UIButton){
addSubview(point1view)
addConstraintsWithFormat("H:|-20-[v0]-20-|", views: point1view)
//TODO anytime i add a constraint here the background color leaves!
print(subviews[subviews.count-1].backgroundColor) //Prints white
}
}
更新类:我想也许这是与此相关的问题:
UITableViewCell subview disappears when cell is selected
凡UICollectionViewCell
被选中,因此iOS的自动设置的backgroundColor清除。问题是,我实现了UIView
的这个类扩展,看看在backgroundColor
上调用了didSet
,当它被设置为清除时,我将它设置为白色。但是,它只在backgroundColor上调用didSet
一次,当我第一次设置视图的颜色。这是我用于重写UIView
类的代码:
class NeverClearView: UIView {
override var backgroundColor: UIColor? {
didSet {
print("background color is being set")
if backgroundColor == UIColor.clearColor() {
print("set to a clear color")
backgroundColor = UIColor.whiteColor()
}
}
}
}
你看到的差异被明显由产生零宽度或零高度的视图帧引起的。
让我们来解释绘图系统是如何工作的。
每个视图都有一个图层,该图层在由视图框架指定的边界中绘制其背景色。然后绘制每个子视图。但是,子视图不受限制,除非您将UIView.clipsToBounds
设置为true
。
您所看到的是指容器视图具有零框架(宽度或高度),但其子视图具有正确的框架,因此它们显示正确。
有多种原因,这可能发生,例如:
- 要设置
translatesAutoresizingMaskIntoConstraints
到false
一些系统视图(例如在UICollectionView
的内容视图)。 - 你有一个约束冲突,导致一些重要的约束被删除(你应该看到一个警告)。
- 您错过了一些限制。具体来说,我没有看到你设置垂直约束。
您应该可以使用Xcode中的视图调试器来调试问题。只需打开您的应用程序,单击视图调试器按钮并打印单元格的递归描述。你应该看到一个零框架。
什么是背景? – ldindu
'backgroundColor'是子视图的背景颜色,'point1view',我将其作为子视图添加到我的'UiCollectionViewCell'中。 – Thalatta
你的意思是什么消失了? – ldindu