如何滚动我的 UIViewController 内容?

How to scroll my UIViewController content?

我一直在尝试搜索 swift 版本的解决方案,但无济于事。我想知道如何让我的视图控制器滚动内容。现在它只是静态的,下面还有更多内容,我无法滚动。

class ProfileViewController: UIViewController, UICollectionViewDelegateFlowLayout {
    let postsId = "postsId"
    override func viewDidLoad() {
    super.viewDidLoad()


    view.addSubview(profileContainerView)
    view.addSubview(profileTabCollection)
    view.addSubview(containerForCollectionView)
    view.addSubview(scrollView)

    scrollView.widthAnchor.constraintEqualToConstant(view.frame.size.width).active = true
    scrollView.heightAnchor.constraintEqualToConstant(view.frame.size.height).active = true
    scrollView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true
    scrollView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor).active = true

    scrollView.userInteractionEnabled = true
    scrollView.scrollEnabled = true
    scrollView.delegate = self


    containerForCollectionView.widthAnchor.constraintEqualToConstant(scrollView.frame.size.width).active = true
    containerForCollectionView.heightAnchor.constraintEqualToConstant(scrollView.frame.size.height).active = true
    containerForCollectionView.topAnchor.constraintEqualToAnchor(profileTabCollection.bottomAnchor).active = true

    profileContainerView.widthAnchor.constraintEqualToConstant(scrollView.frame.size.width).active = true
    profileContainerView.heightAnchor.constraintEqualToConstant(250).active = true
    profileContainerView.centerXAnchor.constraintEqualToAnchor(scrollView.centerXAnchor).active = true
    profileContainerView.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor).active = true

    profileTabCollection.widthAnchor.constraintEqualToAnchor(profileContainerView.widthAnchor).active = true
    profileTabCollection.heightAnchor.constraintEqualToConstant(50).active = true
    profileTabCollection.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true
    profileTabCollection.topAnchor.constraintEqualToAnchor(profileContainerView.bottomAnchor).active = true

}

lazy var scrollView: UIScrollView = {
    let sv = UIScrollView(frame: CGRectMake(0,0,1024,768))
    sv.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * 3)
    sv.translatesAutoresizingMaskIntoConstraints = false
    return sv
}()


let containerForCollectionView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.purpleColor()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

在覆盖 layoutSubviews() 方法中写入这些行。

scrollView.widthAnchor.constraintEqualToAnchor(view.widthAnchor).active = true
scrollView.heightAnchor.constraintEqualToAnchor(view.heightAnchor).active = true

确保您在 UIViewController 中创建了 UIScrollView,否则肯定无法正常工作。

这行不通的原因有很多。由于您没有提供足够的信息来确定它是什么,我建议您仔细阅读这篇文章(尤其是 Duncan C 的回答)。

如果您正在使用 swift 2.0 并且您已经使用 "UIScrollViewDelegate"- 您需要添加此方法

   func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imgview
    }

如果您使用的是 swift 3.0 - 您需要此功能

func viewForZooming( in scrollView: UIScrollView) -> UIView? {
        return imgview
    }