2

I try to add some UIView objects as subviews to a UIScrollView and give them random color. But these UIView objects' backgroundColor are white. Why can't I set their backgroundColor?

I run this project on iOS 10.1, Swift 3.0.1 and SnapKit 3.0.2. Here is code in viewDidLoad:

let scrollView = UIScrollView.init()
scrollView.isPagingEnabled = true

self.view.addSubview(scrollView)
scrollView.snp.makeConstraints { (make) in
    make.left.right.top.bottom.equalTo(self.view)
}

var lastView: UIView? = nil
for i in 0 ..< 5 {
    let colorView = UIView.init()
    let red = CGFloat(arc4random() % 256) / 255.0
    let blue = CGFloat(arc4random() % 256) / 255.0
    let green = CGFloat(arc4random() % 256) / 255.0
    colorView.backgroundColor = UIColor.init(red: red, green: green, blue: blue, alpha: 1.0)
    scrollView.addSubview(colorView)

    colorView.snp.makeConstraints({ (make) in
        make.top.bottom.equalTo(scrollView)
        make.width.equalTo(scrollView)

        if i == 0 {
            make.left.equalTo(scrollView)
        }
        else {
            make.left.equalTo((lastView?.snp.right)!)
        }

        if i == 4 {
            make.right.equalTo(scrollView)
        }
    })
    lastView = colorView
}

I think the problem is that the backgroundColor of scrollView will cover on the `colorView'.

songlebao
  • 73
  • 1
  • 7
  • What is the position and size of view you are initializing while " let colorView = UIView.init()" ? I think it might be the problem. Your view is not being added to the scroll view – Nitesh Kumar Singh Nov 02 '16 at 09:02
  • Not that there's anything wrong with `CGFloat(arc4random() % 256) / 255.0` but rather than that, can you just test with `.redColor()`? ... And are you sure the constraints are right? I'd imagine it's one of those two things rather than not being able to set the bg colour... I can't see a height constraint. – Magoo Nov 02 '16 at 09:28
  • @Nitesh Kumar Singh Thanks. I use the SnapKit to set the constraints of `colorView`. I am very sure that it's added to the `scrollView`. – songlebao Nov 03 '16 at 01:07
  • @Magoo I also have tried to use `UIColor.red`. I still have this problem. – songlebao Nov 03 '16 at 01:10

1 Answers1

0

Do you potentially need to add make.height.equalTo(scrollView) ... I don't see a height constraint...

Magoo
  • 2,552
  • 1
  • 23
  • 43
  • Thanks for your answer. But I have this line of code `make.top.bottom.equalTo(scrollView)` which will automatically set the height to be equal to scrollView – songlebao Nov 03 '16 at 01:04
  • You're right. I change `make.top.bottom.equalTo(scrollView)` to `make.top.height.equalTo(scrollView)` and the problem is solved. [Robert Payne](https://github.com/robertjpayne) at SnapKit points out that "right and bottom attributes in scroll views are for getting scroll bounds to work". – songlebao Nov 03 '16 at 07:50
  • No problem, constraints can be funny like that. I have a similar library and I find stuff like this happening a lot. – Magoo Nov 03 '16 at 11:00