向子视图添加约束会使背景颜色不显示

时间:2016-07-25 21:19:28

标签: ios swift uiview

所以我使用自定义函数来格式化我添加到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设置为clear。问题是,我实现了UIView的此类扩展,以查看didSet上何时调用backgroundColor,当它设置为清除时,我将其设置为白色。但是,当我第一次设置视图的颜色时,它只会在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()
         }
      }
   }
}

1 个答案:

答案 0 :(得分:7)

您看到的差异显然是由视图框架导致零宽度或零高度造成的。

让我们解释绘图系统的工作原理。

每个视图都有一个图层,在其边界中绘制背景颜色,由视图框指定。然后绘制每个子视图。但是,除非您将UIView.clipsToBounds设置为true,否则子视图不受框架的限制。

您所看到的意味着容器视图具有零帧(宽度或高度),但其子视图具有正确的帧,因此它们可以正确显示。

出现这种情况的原因有多种,例如:

  1. 您正在将translatesAutoresizingMaskIntoConstraints设置为false到某个系统视图(例如UICollectionView的内容视图)。
  2. 你有一个约束冲突,导致一些重要的约束被删除(你应该看到一个警告)。
  3. 您缺少一些约束。具体来说,我没有看到你设置垂直约束。
  4. 您应该能够使用Xcode中的视图调试器来调试问题。只需打开您的应用程序,单击视图调试器按钮并打印单元格的递归描述。你应该看到一个零的框架。