I am running Xcode 11 and would like to be able to change the background color of a UIScrollView as it moves from page to page in a 6 page setup. I have been able to change the label content for each page but the background color 'skips' to the last in the color array.
The arrays:
let stringArray = ["<- Swipe to begin", "Welcome", "Info", "more info", "more info 2", "end"]
let backgroundColorArray = [UIColor.orange, UIColor.blue, UIColor.red, UIColor.white, UIColor.green, UIColor.purple]
Then the scrollview content
func setupScrollView() {
scrollView.delegate = self
scrollView.contentSize = CGSize(width: self.view.frame.size.width * 6, height: scrollView.frame.size.height)
for k in 0 ... 5 {
scrollView.backgroundColor = backgroundColorArray[k]
}
for i in 0 ... 5 {
let label = UILabel(frame: CGRect(x: scrollView.center.x + CGFloat(i) * self.view.frame.size.width - 130, y: 50, width: 260, height: 30))
label.font = UIFont.boldSystemFont(ofSize: 26)
label.textAlignment = .center
label.text = stringArray[i]
scrollView.addSubview(label)
}
}
The scrollview background color changes to purple but does not change from page to page. What is the correct way to have the colors change?