【问题标题】:IOS Swift4 Not able to scroll a ScrollViewIOS Swift4无法滚动ScrollView
【发布时间】:2021-09-12 14:41:44
【问题描述】:

我一直试图让滚动视图滚动,只是滚动视图应该显示的程度。但是,我做不到。这是我的代码。

func setupMainView() {
    // This is where the image view and other UIViews which are supposed to go in the contentview are set up
    self.setupImagesView()
    self.setupView1()
    self.setupView2()
    self.setupView3()
    self.setupView4()

    self.scrollView = UIScrollView()
    self.scrollView.backgroundColor = UIColor.white
    self.view.addSubview(self.scrollView)
    self.automaticallyAdjustsScrollViewInsets = false
    self.scrollView.contentInset = UIEdgeInsets.zero
    self.scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;

    self.contentView = UIView()
    self.scrollView.layer.masksToBounds = false
    self.scrollView.backgroundColor = UIColor.white
    self.scrollView.layer.borderWidth = 0
    self.scrollView.layer.borderColor = UIColor.white.cgColor
    self.view.addSubview(scrollView)

    self.contentView.addSubview(imagesScrollView)
    self.contentView.addSubview(view1)
    self.contentView.addSubview(view2)
    self.contentView.addSubview(view3)
    self.contentView.addSubview(view4)
    self.scrollView.addSubview(contentView)
    self.scrollView.contentInset = UIEdgeInsets.zero
    self.scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;

    var scrollViewHeight:CGFloat = 0.0;

    for _ in self.scrollView.subviews {
        scrollViewHeight += view.frame.size.height
    }
    var newHeight = scrollViewHeight * 1.1 + offset + 100
    scrollView.contentSize = CGSize(width:screenWidth, height:newHeight)
    scrollView.reloadInputViews()
}

视图正在加载等,但我不管理滚动。不知何故,要么太少,要么太多。

现在,我尝试将 contentSize 的高度设置为 scrollViewHeight 并将其设置为两倍等。我注意到无法预测它将滚动多少。从 1.1 更改为 1.6 .. 视图下方的白屏太多,将其更改为 1.1 或 1.2 它甚至不会滚动到底部。

注意,一切都是以编程方式设置的,没有情节提要等。 另请注意,我需要支持所有版本 > 10 的 IOS 设备。

我有点迷路了。我做错了什么?

【问题讨论】:

  • 如果我没记错,那么您将在 self.viiew self.view.addSubview(scrollView) 中添加两次滚动视图。请检查
  • 不错,但这似乎不是问题所在。尝试注释掉任何一行,但仍然无法正常工作。
  • 您没有使用 AutoLayout 的任何特殊原因?
  • 我没有使用 AutoLayout。会试一试,如果这不起作用。为什么这不起作用?

标签: ios swift uiscrollview


【解决方案1】:

这是配置滚动视图的一种非常古老的方式 - 您应该使用自动布局。

而且,你做错了很多事情......

首先,我们假设您在未显示的代码中设置各种子视图的框架。

但是,您拥有显示的代码创建了一个滚动视图并将其添加到self.view——但您从未设置滚动视图的框架。

另外,这部分代码:

for _ in self.scrollView.subviews {
    scrollViewHeight += view.frame.size.height
}

您已将多个视图添加为contentView 的子视图,然后将contentView 添加为scrollView唯一子视图

而且...您正在尝试将scrollViewHeight 增加 根视图 的高度,而不是滚动视图的高度子视图。

所以,scrollViewHeight 将只有 self.view 的高度。

您可能想要做的是将contentView.subviews 的高度相加:

    var contentViewHeight: CGFloat = 0
    
    for v in contentView.subviews {
        contentViewHeight += v.frame.height
    }
    contentView.frame.size.height = contentViewHeight

然后将scrollView的contentSize.height设置为contentView的frame的高度。

这是一个非常非常基本的示例,使用显式设置的帧大小 -- 不过,您应该再次开始使用自动布局:

class SimpleScrollViewController: UIViewController {

    var imagesScrollView: UIView!
    var view1: UIView!
    var view2: UIView!
    var view3: UIView!
    var view4: UIView!

    var contentView: UIView!
    var scrollView: UIScrollView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupMainView()
    }
    func setupMainView() {
        // This is where the image view and other UIViews which are supposed to go in the contentview are set up
        self.setupImagesView()
        self.setupView1()
        self.setupView2()
        self.setupView3()
        self.setupView4()
        
        self.scrollView = UIScrollView()
        
        // let's use a color other than white so we can see the frame of the scrollView
        self.scrollView.backgroundColor = UIColor.cyan
        
        self.view.addSubview(self.scrollView)
        self.automaticallyAdjustsScrollViewInsets = false
        self.scrollView.contentInset = UIEdgeInsets.zero
        self.scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;
        
        self.contentView = UIView()
        self.contentView.backgroundColor = UIColor.lightGray
        
        self.scrollView.layer.masksToBounds = false
        self.scrollView.layer.borderWidth = 0
        self.scrollView.layer.borderColor = UIColor.white.cgColor
        self.view.addSubview(scrollView)
        
        self.contentView.addSubview(imagesScrollView)
        self.contentView.addSubview(view1)
        self.contentView.addSubview(view2)
        self.contentView.addSubview(view3)
        self.contentView.addSubview(view4)
        self.scrollView.addSubview(contentView)
        self.scrollView.contentInset = UIEdgeInsets.zero
        self.scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;
        
        var contentViewHeight: CGFloat = 0
        
        for v in contentView.subviews {
            contentViewHeight += v.frame.height
        }
        contentView.frame.size.height = contentViewHeight
        
        // don't know what you're doing here....
        //scrollView.reloadInputViews()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        // here is where you know the frame of self.view
        //  so, make the scroll view cover the entire view
        scrollView.frame = view.frame
        
        // now, make contentView width equal to scrollView width
        contentView.frame.size.width = scrollView.frame.size.width
        
        // set the scrollView's content size
        scrollView.contentSize = contentView.frame.size
    }
    
    func setupImagesView() -> Void {
        imagesScrollView = UIView()
        imagesScrollView.backgroundColor = .red
        imagesScrollView.frame =  CGRect(0, 0, 300, 100)
    }
    func setupView1() -> Void {
        view1 = UIView()
        view1.backgroundColor = .green
        view1.frame = CGRect(20, imagesScrollView.frame.maxY, 300, 200)
    }
    func setupView2() -> Void {
        view2 = UIView()
        view2.backgroundColor = .blue
        view2.frame = CGRect(40, view1.frame.maxY, 300, 250)
    }
    func setupView3() -> Void {
        view3 = UIView()
        view3.backgroundColor = .yellow
        view3.frame = CGRect(60, view2.frame.maxY, 200, 275)
    }
    func setupView4() -> Void {
        view4 = UIView()
        view4.backgroundColor = .orange
        view4.frame = CGRect(80, view3.frame.maxY, 200, 100)
    }

}

【讨论】:

    【解决方案2】:

    如果我没记错的话,你需要为了让滚动视图工作,你需要实现几个委托方法。您还需要设置几个属性。

    contentSize 为一 我认为最小和最大尺寸

    见:https://developer.apple.com/documentation/uikit/uiscrollviewdelegate

    另见斯坦福大学的 Paul Hagarty 使用 Swift 第 9 集开发 IOS 11 应用程序以获取有关 UIScrollView 的大量信息

    https://www.youtube.com/watch?v=B281mrPUGjg

    寻找大约 31 分钟的滚动视图信息。

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多