Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed stack view in scroll view programmatically

I have tried embedding it, but my stack view is dynamic and my app is also changing orientations time to time. I have segment control at the end of the view. I have also tried googling it but had no luck. thanks in advance.

So far I have done:

In view did load:

mainStackView.axis = UILayoutConstraintAxis.Vertical
mainStackView.spacing = 3
scrollView.frame =  self.view.bounds
scrollView.addSubview(mainStackView)
view.addSubview(scrollView)

In view did layout:

override func viewDidLayoutSubviews()
    {
        super.viewDidLayoutSubviews()
        let top = topLayoutGuide.length
        let bottom = bottomLayoutGuide.length
                        self.mainStackView.frame = CGRect(x: 0, y: top, width: view.frame.width, height: view.frame.height - top - bottom).insetBy(dx: 10, dy: 10)
        dispatch_async(dispatch_get_main_queue())
            {

                self.scrollView.frame =  self.view.bounds
                self.scrollView.contentSize = CGSize(width: self.view.bounds.width, height: self.segmentedControl.frame.origin.y + self.segmentedControl.frame.height + 50)
        }
        print(scrollView.contentSize)
    }
like image 807
shafi Avatar asked Oct 30 '22 10:10

shafi


1 Answers

You need to set the height constraint of segment control.

For Example:

 segmentedControl.heightAnchor.constraintEqualToConstant(50).active = true

More over, you can Add Empty bottom view to avoid stack view's must fill mechanism. This will show you desired view output.

        var bottomView = UIView(frame: CGRectZero)
        stackView.addArrangedSubview(bottomView)
like image 116
Samraan Khaan Avatar answered Nov 14 '22 10:11

Samraan Khaan