ios – 在Swift 3中以编程方式使用ScrollView

前端之家收集整理的这篇文章主要介绍了ios – 在Swift 3中以编程方式使用ScrollView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经搜索了其他问题,似乎仍然在使用 swift 3中的autolayout以编程方式创建我的scrollView时遇到了一些麻烦.我能够让我的scrollview显示如下图所示,但是当我滚动到底部时我的其他标签确实如此没有显示,“滚动顶部”标签不会消失.

希望有人可以帮助我们查看下面的代码

import UIKit

class ViewController: 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
}()


override func viewDidLoad() {
    super.viewDidLoad()

    let screensize: CGRect = UIScreen.main.bounds
    let screenWidth = screensize.width
    let screenHeight = screensize.height
    var scrollView: UIScrollView!
    scrollView = UIScrollView(frame: CGRect(x: 0,y: 120,width: screenWidth,height: screenHeight))
    scrollView.contentSize = CGSize(width: screenWidth,height: 2000)
    scrollView.addSubview(labelOne)
    scrollView.addSubview(labelTwo)


    view.addSubview(labelOne)
    view.addSubview(labelTwo)
    view.addSubview(scrollView)

    // Visual Format Constraints
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|",options: NSLayoutFormatOptions(),metrics: nil,views: ["v0": labelOne]))
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-100-[v0]",views: ["v0": labelOne]))

    // Using iOS 9 Constraints in order to place the label past the iPhone 7 view
    view.addConstraint(NSLayoutConstraint(item: labelTwo,attribute: .top,relatedBy: .equal,toItem: labelOne,attribute: .bottom,multiplier: 1,constant: screenHeight + 200))
    view.addConstraint(NSLayoutConstraint(item: labelTwo,attribute: .right,constant: 0))
    view.addConstraint(NSLayoutConstraint(item: labelTwo,attribute: .left,constant: 0)     

    }

}

解决方法

使用约束来定义滚动内容大小很容易 – 因此您不必进行任何手动计算.

只记得:

>滚动视图的内容元素必须具有左/上/宽度/高度值.对于标签等对象,它们具有固有的大小,因此您只需要定义左侧和右侧.最佳.
>滚动视图的内容元素也定义了可滚动区域的边界 – contentSize – 但是它们使用底部底部来定义.正确的约束.
>结合这两个概念,您会发现需要一个“连续链”,其中至少有一个元素定义了顶部/左侧/底部/右侧范围.

这是一个简单的示例,它将直接在Playground页面中运行:

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
原文链接:https://www.f2er.com/iOS/335126.html

猜你在找的iOS相关文章