Search code examples
swiftuiscrollview

Scroll view not scrolling horizontal


I have a custom view, i set it in parent like:

func setup(){
    view.backgroundColor = .gray
    view.addSubview(chartView)
    chartView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    chartView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    chartView.topAnchor.constraint(equalTo: view.topAnchor, constant: statusAndNavigationBarHeight).isActive = true
    chartView.heightAnchor.constraint(equalToConstant: Dimensions.chartHeight.value).isActive = true
}

Then in that view i tried to set up a scroll:

scroll = UIScrollView.init(frame: CGRect(x: 0.0, y: 0.0, width: scrollWidth(), // print 728.0
                                                 height: Double(Dimensions.chartHeight.value))) // print 400.0
    scroll.isScrollEnabled = true
    scroll.showsHorizontalScrollIndicator = true
    addSubview(scroll)
}

And thats all, when i launch app i can't drag and scroll horizontally, in debug editor i can't see that it is scroll view here lying with large width.


Solution

  • The scrollView doesn't scroll with it's size , it needs a content that define it's content size for example

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            let chartView = UIView()
            chartView.translatesAutoresizingMaskIntoConstraints = false
            chartView.backgroundColor = .red
            view.backgroundColor = .gray
            view.addSubview(chartView)
            chartView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
            chartView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
            chartView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
            chartView.heightAnchor.constraint(equalToConstant:200).isActive = true
            let scroll = UIScrollView(frame: CGRect(x: 0.0,
                y: 0.0,
                width: UIScreen.main.bounds.size.width, // print 728.0
                height: 200.0))
            scroll.isScrollEnabled = true
            scroll.showsHorizontalScrollIndicator = true
            chartView.addSubview(scroll)
            let www = UIView()
            www.backgroundColor = .green
            www.translatesAutoresizingMaskIntoConstraints = false
             scroll.addSubview(www)
            www.leftAnchor.constraint(equalTo: scroll.leftAnchor).isActive = true
            www.rightAnchor.constraint(equalTo: scroll.rightAnchor).isActive = true
            www.topAnchor.constraint(equalTo: scroll.topAnchor).isActive = true
            www.bottomAnchor.constraint(equalTo: scroll.bottomAnchor).isActive = true
            www.heightAnchor.constraint(equalToConstant:200).isActive = true
            www.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier:2.0).isActive = true
            scroll.addSubview(www)
    
        }
    }
    

    enter image description here