所以我使用自定义函数来格式化我添加到a的子视图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))
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,在我的UICollectionView
I中添加一个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)
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,print(subviews)
我看到UIView
白色背景颜色是视图堆栈中最高的(堆栈顶部).当我打印出来时,subviews[subviews.count-1].backgroundColor
我得到的Optional(UIDeviceWhiteColorSpace 1 1)
是我所期望的.这很奇怪,因为没有显示颜色.
我不知道如何看看幕后发生的事情,以确认在后一种情况下背景是完全设置的.
这一切都发生在UiCollectionViewCell
我正在使用的一个类中,作为我的一个UICollectionView
Cell 的类,可以在这里完整地查看:
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
}
}
Run Code Online (Sandbox Code Playgroud)
更新:我想也许这与这个问题有关:
凡UICollectionViewCell
被选中,因此iOS的自动设置的backgroundColor清除.问题是,我实现了这个类的扩展,UIView
以查看何时didSet
被调用backgroundColor
,当它被设置为清除时,我将其设置为白色.但是,didSet
当我第一次设置视图的颜色时,它只调用backgroundColor一次.这是我用来覆盖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()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您看到的差异显然是由视图框架导致零宽度或零高度引起的.
让我们解释一下绘图系统的工作原理.
每个视图都有一个图层,用于在视图框架指定的边界中绘制背景颜色.然后绘制每个子视图.但是,除非您设置UIView.clipsToBounds
为,否则子视图不受框架的限制true
.
您所看到的意味着容器视图具有零帧(宽度或高度),但其子视图具有正确的帧,因此它们正确显示.
有多种原因导致这种情况发生,例如:
translatesAutoresizingMaskIntoConstraints
到false
某些系统视图(例如的内容视图UICollectionView
).您应该能够使用Xcode中的视图调试器来调试问题.只需打开您的应用程序,单击视图调试器按钮并打印单元格的递归描述.你应该看到一个零的框架.
归档时间: |
|
查看次数: |
2024 次 |
最近记录: |