【问题标题】:adding a constraint to a subview makes background color not display向子视图添加约束会使背景颜色不显示
【发布时间】:2016-11-29 09:03:08
【问题描述】:

所以我使用自定义函数来格式化我添加到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()
         }
      }
   }
}

【问题讨论】:

  • 什么是背景?
  • backgroundColor 是子视图的背景色,point1view 我将其作为子视图添加到我的UiCollectionViewCell
  • 消失了是什么意思?
  • 对不起,我使用的语言令人困惑。我的意思是当我添加约束时,我分配给这个特定子视图的whiteColor() 不会出现(而不是消失)。在那种情况下,它永远不会存在:)
  • 那么显然你的约束有问题。

标签: ios swift uiview


【解决方案1】:

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

让我们解释一下绘图系统是如何工作的。

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

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

发生这种情况的原因有多种,例如:

  1. 您正在将translatesAutoresizingMaskIntoConstraints 设置为false 到某个系统视图(例如UICollectionView 的内容视图)。
  2. 您有一个约束冲突,导致一些重要的约束被删除(您应该会看到一个警告)。
  3. 您缺少一些约束。具体来说,我没有看到您设置垂直约束。

您应该能够使用 Xcode 中的视图调试器来调试问题。只需打开您的应用程序,单击查看调试器按钮并打印单元格的递归描述。您应该会看到一个为零的帧。

【讨论】:

    猜你喜欢
    • 2020-08-12
    • 1970-01-01
    • 2020-11-10
    • 2015-06-29
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    相关资源
    最近更新 更多