Using ScrollView Programmatically in Swift 3

后端 未结 5 592
执念已碎
执念已碎 2020-11-28 11:00

I have searched other questions and seem to still have some trouble creating my scrollView programmatically with autolayout in swift 3. I am able to get my scrollview to sho

5条回答
  •  Happy的楠姐
    2020-11-28 11:37

    It is easy to use constraints to define the scroll content size - so you don't have to do any manual calculations.

    Just remember:

    1. The content elements of your scroll view must have left / top / width / height values. In the case of objects such as labels, they have intrinsic sizes, so you only have to define the left & top.
    2. The content elements of your scroll view also define the bounds of the scrollable area - the contentSize - but they do so with the bottom & right constraints.
    3. Combining those two concepts, you see that you need a "continuous chain" with at least one element defining the top / left / bottom / right extents.

    Here is a simple example, that will run directly in a Playground page:

    import UIKit
    import PlaygroundSupport
    
    class TestViewController : UIViewController {
    
        let labelOne: UILabel = {
            let label = UILabel()
            label.text = "Scroll Top"
            label.backgroundColor = .red
            label.translatesAutoresizingMaskIntoConstraints = false
            return label
        }()
    
        let labelTwo: UILabel = {
            let label = UILabel()
            label.text = "Scroll Bottom"
            label.backgroundColor = .green
            label.translatesAutoresizingMaskIntoConstraints = false
            return label
        }()
    
        let scrollView: UIScrollView = {
            let v = UIScrollView()
            v.translatesAutoresizingMaskIntoConstraints = false
            v.backgroundColor = .cyan
            return v
        }()
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // add the scroll view to self.view
            self.view.addSubview(scrollView)
    
            // constrain the scroll view to 8-pts on each side
            scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 8.0).isActive = true
            scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 8.0).isActive = true
            scrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -8.0).isActive = true
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8.0).isActive = true
    
            // add labelOne to the scroll view
            scrollView.addSubview(labelOne)
    
            // constrain labelOne to left & top with 16-pts padding
            // this also defines the left & top of the scroll content
            labelOne.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 16.0).isActive = true
            labelOne.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 16.0).isActive = true
    
            // add labelTwo to the scroll view
            scrollView.addSubview(labelTwo)
    
            // constrain labelTwo at 400-pts from the left
            labelTwo.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 400.0).isActive = true
    
            // constrain labelTwo at 1000-pts from the top
            labelTwo.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 1000).isActive = true
    
            // constrain labelTwo to right & bottom with 16-pts padding
            labelTwo.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: -16.0).isActive = true
            labelTwo.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -16.0).isActive = true
    
        }
    
    }
    
    
    let vc = TestViewController()
    vc.view.backgroundColor = .yellow
    PlaygroundPage.current.liveView = vc
    

提交回复
热议问题