私は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
を追加し、背景色を白に設定することです。サブビューの背景を設定する行をコメントアウトすると背景が白くなり、サブビューの視覚的にフォーマットされた制約を設定する行のコメントを外すと背景色は設定されません。私は白い背景色でUIView
ビュースタック(スタックの最上部)で最大となることprint(subviews)
参照ないとき
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)
}
:ここ
は、互いに壊し2行があります。 subviews[subviews.count-1].backgroundColor
を印刷すると、私は期待しているものがOptional(UIDeviceWhiteColorSpace 1 1)
になります。色が表示されないので奇妙です。
バックグラウンドが後者の場合に設定されていることを確認するために、バックグラウンドで何が起こっているのかを確認する方法がわかりません。
このすべては、私はここにその全体を見ることができる私のUICollectionView
細胞の一つのクラスとして使用していUiCollectionViewCell
のクラスで行われます。ここ
https://gist.github.com/ebbnormal/edb79a15dab4797946e0d1f6905c2dd0
ことからのスクリーンショットですどちらの場合も、最初のケースは行addConstraintsWithFormat
がコメントアウトされており、2番目のケースはコメントアウトされています。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
}
}
UPDATEをオーバーライドするクラスで起こる:
UITableViewCell subview disappears when cell is selected
UICollectionViewCell
が選択されているので、iOSのは、自動的にbackgroundColorのは設定しています。私はそれがこの問題に関連していたかもしれないと思いましたクリアする。問題は、UIView
のこのクラス拡張を実装して、didSet
がbackgroundColor
で呼び出されたときを見て、クリアに設定されているときに白に設定することです。ただし、最初に背景の色を設定すると、didSet
がbackgroundColorに1回だけ呼び出されます。ここで私は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'はサブビューの背景色です。私は' UiCollectionViewCell'にサブビューとして追加している 'point1view'です – Thalatta
そしてそれが消えるのはどういう意味ですか? – ldindu