2016-07-25 103 views
13

所以我使用自定義函數來格式化我添加到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的子視圖在第一種情況下以白色背景突出顯示。

enter image description here

enter image description here

這是我如何設置的意見。這一切都發生在重寫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() 
     } 
     } 
    } 
} 
+0

什麼是背景? – ldindu

+0

'backgroundColor'是子視圖的背景顏色,'point1view',我將其作爲子視圖添加到我的'UiCollectionViewCell'中。 – Thalatta

+0

你的意思是什麼消失了? – ldindu

回答

6

你看到的差異被明顯由產生零寬度或零高度的視圖幀引起的。

讓我們來解釋繪圖系統是如何工作的。

每個視圖都有一個圖層,該圖層在由視圖框架指定的邊界中繪製其背景色。然後繪製每個子視圖。但是,子視圖不受限制,除非您將UIView.clipsToBounds設置爲true

您所看到的是指容器視圖具有零框架(寬度或高度),但其子視圖具有正確的框架,因此它們顯示正確。

有多種原因,這可能發生,例如:

  1. 要設置translatesAutoresizingMaskIntoConstraintsfalse一些系統視圖(例如在UICollectionView的內容視圖)。
  2. 你有一個約束衝突,導致一些重要的約束被刪除(你應該看到一個警告)。
  3. 您錯過了一些限制。具體來說,我沒有看到你設置垂直約束。

您應該可以使用Xcode中的視圖調試器來調試問題。只需打開您的應用程序,單擊視圖調試器按鈕並打印單元格的遞歸描述。你應該看到一個零框架。