所以我使用自定義函數來格式化我添加到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()
}
}
}
}
什麼是背景? – ldindu
'backgroundColor'是子視圖的背景顏色,'point1view',我將其作爲子視圖添加到我的'UiCollectionViewCell'中。 – Thalatta
你的意思是什麼消失了? – ldindu