Reset subviews in segmented control

338 Views Asked by At

I am using a segmented control to rotate through content, but when I scroll to the end of a subview and switch to another segment, the new segment shows roughly in the same position as the previous segment (i.e. if I scroll to the bottom of segment A and switch segments, segment B is also at the bottom of the scroll). How can I reset each segment so that it is positioned at the top each time I change segments?

Sample code:

class AboutUsViewController: UIViewController {        
    @IBOutlet weak var welcomeContainer: UIView!
    var views: [UIView]!

    override func viewDidLoad() {
        super.viewDidLoad()

        views = [UIView]()
        views.append(ViewController1().view)
        views.append(ViewController2().view)
        views.append(ViewController3().view)
        views.append(ViewController4().view)

        for view in views {
            welcomeContainer.addSubview(view)
        }

        welcomeContainer.bringSubview(toFront: views[0])   
    }

    @IBAction func swichViewAction(_ sender: UISegmentedControl) {
        self.welcomeContainer.bringSubview(toFront: views[sender.selectedSegmentIndex])
    }
}
0

There are 0 best solutions below