Swift卷轴不滚动

时间:2017-03-21 08:45:53

标签: swift cocoa-touch uiscrollview

我有一个Swift代码

    background_img_view.addSubview(background_image)
    background_img_view.addSubview(usn)
    profile_view.addSubview(PostsDiv)
    profile_view.addSubview(PostDiv)
    var scrollView: UIScrollView!
    scrollView = UIScrollView(frame: view.bounds)
    scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 1500)
    scrollView.autoresizingMask = UIViewAutoresizing.flexibleHeight
    scrollView.isScrollEnabled = true
    scrollView.addSubview(profile_view)
    view.addSubview(background_img_view)
    view.addSubview(scrollView)

我在手机上测试了这个并且滚动条正在显示,但是当我滚动没有任何变化时,我也无法点击屏幕上的任何内容

1 个答案:

答案 0 :(得分:0)

您将所有内容锚定在背景图像的底部,但背景图像不在滚动视图中,因此滚动时不会移动任何内容。

您需要将view.addSubview(background_img_view)替换为scrollView.addSubview(background_img_view)

您还需要将背景图片视图的顶部锚点和背景图像更改为scrollView的顶部。

替换:

background_img_view.anchor(view.topAnchor, left:view.leftAnchor, bottom: about.bottomAnchor, right: view.rightAnchor, topConstant: 60, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 280)
background_image.anchor(view.topAnchor, left:view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 60, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 280)

使用

background_img_view.anchor(scrollView.topAnchor, left:view.leftAnchor, bottom: about.bottomAnchor, right: view.rightAnchor, topConstant: 60, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 280)
background_image.anchor(scrollView.topAnchor, left:view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 60, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 280)
相关问题