I have a UIScrollView
that has UIStackView
with three objects inside it (label, image and label)
How do I let the scrollview work with the stackview?
As you see in code I added the Label11,image1 and label12 to the stack but I want the stack items to change when scrolling to label21, image2 and label22 and I don't know how!
How can I do that? Here is what I have tried:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// parent scroll
let ScrollView = UIScrollView()
ScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, 667)
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 667)
ScrollView.backgroundColor = UIColor.init(colorLiteralRed: 219, green: 237, blue: 254, alpha: 1)
ScrollView.pagingEnabled = true
//label1
let label11 = UILabel()
label11.text = "label1"
// image1
let image1 = UIImageView()
image1.frame = CGRectMake(self.view.frame.size.width * 1, 0, self.view.frame.size.width, 200)
image1.image = UIImage(named: "mother1")
//label2
let label12 = UILabel()
label12.text = "label2"
//label21
let label21 = UILabel()
label21.text = "label1"
// image2
let image = UIImageView()
image.frame = CGRectMake(self.view.frame.size.width * 1, 0, self.view.frame.size.width, 200)
image.image = UIImage(named: "mother1")
//label22
let label22 = UILabel()
label22.text = "label2"
//Stackview
let stackView = UIStackView(arrangedSubviews: [label11, image1, label12])
stackView.distribution = .FillEqually
stackView.axis = .Vertical
stackView.alignment = .Fill
stackView.spacing = 0
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.backgroundColor = UIColor.blueColor()
ScrollView.addSubview(stackView)
let margins = ScrollView.layoutMarginsGuide
stackView.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor, constant: 0).active = true
stackView.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor, constant: 0).active = true
stackView.topAnchor.constraintEqualToAnchor(margins.topAnchor, constant: 0).active = true
stackView.bottomAnchor.constraintEqualToAnchor(margins.bottomAnchor, constant: 0).active = true
stackView.widthAnchor.constraintEqualToAnchor(ScrollView.widthAnchor, constant: 0).active = true
stackView.heightAnchor.constraintEqualToAnchor(ScrollView.heightAnchor, constant: 0).active = true
self.view.addSubview(ScrollView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}