Im able to initialize my scrollview and set its contentSize with a background color...everything works fine when thats done but when I utilize the following code to add subviews to the scrollView im unable to see those views...please help?
func setupViews(_ views: [NewView]) {
var multiplier: CGFloat = 0.0
let width = frame.size.width
let height = frame.size.height
for view in views {
let newView = NewView()
addSubview(newView)
newView.frame = CGRect(x: width * multiplier, y: 0, width: width, height: height)
newView.backgroundColor = .random()
newView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
newView.centerXAnchor.constraint(equalTo: centerXAnchor, constant: width * multiplier).isActive = true
multiplier += 1
}
contentSize = CGSize(width: width * multiplier, height: height)
}
I have a commonInit() function in NewView class thats called in both required init & override init , might help so here it is
private func commonInit() {
translatesAutoresizingMaskIntoConstraints = false
backgroundColor = UIColor.white
let labels = [nameLabel, brandLabel, priceLabel]
var multiplier: CGFloat = 0.0
for label in labels {
addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 10.0 * multiplier).isActive = true
label.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0.0).isActive = true
multiplier += 1.0
}
}