0
votes

I am creating scrollView programmatically, but for some reasons i can see the scrollView's scroll bar but the subViews aren't scrolling. Have added my codes below

//create a closure for scroll view
    let vcScrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.backgroundColor = UIColor.dark_background

        return scrollView
    }()

//create closure for dark view
    let darkView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.black
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

//create a closure for offer label
    let offerLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textColor = UIColor.white
        return label
    }()

override func viewDidLoad() {
        super.viewDidLoad()
        modalPresentationCapturesStatusBarAppearance = true

        view.addSubview(vcScrollView)
        setUpViews()


    }

func setUpViews(){
        //scrollView
        vcScrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        vcScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        vcScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        vcScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        vcScrollView.contentSize.height = 1500

        //Dark view
        vcScrollView.addSubview(darkView)
        darkView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        darkView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        darkView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        darkView.heightAnchor.constraint(equalToConstant: 130).isActive = true

        //Offer Label
        darkView.addSubview(offerLabel)
        offerLabel.text = selectedOffer
        offerLabel.font = UIFont(name: String.defaultFontR, size: 29)
        offerLabel.textAlignment = .center
        offerLabel.topAnchor.constraint(equalTo: darkView.topAnchor, constant: 95).isActive = true
        offerLabel.leadingAnchor.constraint(equalTo: darkView.leadingAnchor, constant: 20).isActive = true
        offerLabel.trailingAnchor.constraint(equalTo: darkView.trailingAnchor, constant: -20).isActive = true
    }

Have added the scroll view as a subView for the view too. Thanks in advance

2

2 Answers

0
votes

The problem is your child of the scroll view is using the view's anchors. Switch them to use the scrollView's anchors. Also, instead of a trailing constraint, you can use a width constraint. That will allow it to scroll up and down.

//Dark view
vcScrollView.addSubview(darkView)
darkView.leadingAnchor.constraint(equalTo: vcScrollView.leadingAnchor).isActive = true
darkView.topAnchor.constraint(equalTo: vcScrollView.topAnchor).isActive = true
darkView.widthAnchor.constraint(equalTo: vcScrollView.widthAnchor).isActive = true
darkView.heightAnchor.constraint(equalToConstant: 130).isActive = true

You will need to provide the bottom anchors of your child views if you want to include views under your darkView.

0
votes

I think you should provide the constraint of dark view with scrollview. Currently you are providing the constraint from the view. Like this-

darkView.leadingAnchor.constraint(equalTo: vcScrollView.leadingAnchor).isActive = true

darkView.topAnchor.constraint(equalTo: vcScollView.topAnchor).isActive = true
    darkView.trailingAnchor.constraint(equalTo: vcScollView.trailingAnchor).isActive = true
    darkView.heightAnchor.constraint(equalToConstant: 130).isActive = true

Also provide the bottom constraint so that scollview can take the content size properly.