【发布时间】:2016-11-29 09:03:08
【问题描述】:
所以我使用自定义函数来格式化我添加到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()
}
}
}
}
【问题讨论】:
-
什么是背景?
-
backgroundColor是子视图的背景色,point1view我将其作为子视图添加到我的UiCollectionViewCell -
消失了是什么意思?
-
对不起,我使用的语言令人困惑。我的意思是当我添加约束时,我分配给这个特定子视图的
whiteColor()不会出现(而不是消失)。在那种情况下,它永远不会存在:) -
那么显然你的约束有问题。