【问题标题】:How to scroll my UIViewController content?如何滚动我的 UIViewController 内容?
【发布时间】:2016-08-12 07:14:48
【问题描述】:

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

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
}()

【问题讨论】:

  • scrollView.heightAnchor之后标出scrollView.contentSize = CGSizeMake(view.frame.size.width, view.frame.size.height + 500)
  • 嗯,好像不能滚动
  • 您在使用情节提要吗?约束似乎有些问题。
  • 尝试在viewwillappear中设置contentsize
  • 添加storyboard截图,约束。只有我们才能理解确切的问题。无论你问什么,它都应该是完整的。

标签: ios swift uiscrollview xcode7.2


【解决方案1】:

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

how do i alter the position of a UIImage inside a imageView in SWIFT

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

      如果你使用的是 swift 2.0 并且你使用过“UIScrollViewDelegate”——你需要添加这个方法

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

      如果你使用的是 swift 3.0 - 你需要这个功能

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-16
        • 2020-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多