如何在 UIScrollView 中以编程方式设置 StickyHeader

1 xcode header sticky uiscrollview swift

我正在尝试实现一个粘性标题功能,例如在 twitters 个人资料上看到的那个。我已经根据设置我的滚动视图,我一直在尝试研究如何这样做,但是,我只能找到使用故事板的方法。我的代码将在下面。

class EditProfileVC: UIViewController {
var imageView: UIImageView!
var image = UIImage(named: "work")


lazy var scrollView: UIScrollView = {
    let view = UIScrollView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.contentSize.height = 800
    view.backgroundColor = UIColor.brown

    return view
}()

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(scrollView)
    setupScrollView()
    view.addSubview(profileImageView)
        setupProfileImageView()

}

func setupScrollView(){

    imageView = UIImageView(image: image)
    scrollView.addSubview(imageView)
    scrollView.contentSize = imageView.bounds.size
    view.addSubview(scrollView)

    imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    imageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 200).isActive=true
    imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
    profileImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true

    imageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

    let firstLabel = UILabel()
    firstLabel.translatesAutoresizingMaskIntoConstraints = false
    firstLabel.textColor = .white
    firstLabel.text = "Top of our ScrollView"

    scrollView.addSubview(firstLabel)

    firstLabel.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
    firstLabel.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 20).isActive = true
    firstLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
    firstLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我我遗漏了什么并可能帮助我构建标题吗?如果 Sticky Header 很复杂,UIScrollView 中的一个简单的 header 会有很大帮助。提前谢谢你,约翰 Twitter 个人资料粘性标题 推特个人资料粘性标题

Uph*_*uth 6

像这样的事情应该有效(无论如何对我有用):

class ViewController: UIViewController, UIScrollViewDelegate {
    lazy var imageView: UIImageView = {
        let imageView = UIImageView(image: #imageLiteral(resourceName: "Image"))
        imageView.clipsToBounds = true

        return imageView
    }()

    var imageHeightConstraint: NSLayoutConstraint!

    lazy var scrollView: UIScrollView = {
        let view = UIScrollView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.contentSize = CGSize(width: 1200, height: 1200)
        view.backgroundColor = UIColor.brown
        view.contentInsetAdjustmentBehavior = .never
        view.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)

        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.scrollView.delegate = self
        self.view.addSubview(self.scrollView)
        self.scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        self.scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        self.scrollView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        self.scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

        let testView = UIView()
        testView.backgroundColor = .green
        self.scrollView.addSubview(testView)
        testView.translatesAutoresizingMaskIntoConstraints = false
        testView.leadingAnchor.constraint(equalTo: self.scrollView.leadingAnchor).isActive = true
        testView.trailingAnchor.constraint(equalTo: self.scrollView.trailingAnchor, constant: 150).isActive = true
        testView.topAnchor.constraint(equalTo: self.scrollView.topAnchor).isActive = true
        testView.heightAnchor.constraint(equalToConstant: 100).isActive = true

        self.view.addSubview(self.imageView)
        self.imageView.translatesAutoresizingMaskIntoConstraints = false
        self.imageView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        self.imageView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        self.imageView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true

        self.imageHeightConstraint = self.imageView.heightAnchor.constraint(equalToConstant: 100)
        self.imageHeightConstraint.isActive = true

        self.scrollView.contentOffset = CGPoint(x: 0, y: -100)
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let y = 100 - (scrollView.contentOffset.y + 100)
        let height = max(y, 100)
        self.imageHeightConstraint.constant = height
    }
}
Run Code Online (Sandbox Code Playgroud)