【iOS】scrollView无法滚动解决方法

class LibraryViewController: UIViewController, UIScrollViewDelegate
,UICollectionViewDelegate, UICollectionViewDataSource {

	lazy var scrollView: UIScrollView = {
        let scroll = UIScrollView()
        view.backgroundColor = UIColor.white
        
        let screenFrame = UIScreen.main.bounds
        let screenWidth = screenFrame.size.width
        let screenHeight = screenFrame.size.height
        
        scroll.translatesAutoresizingMaskIntoConstraints = false
        scroll.alwaysBounceVertical = true
        scroll.bounces = true
        scroll.isDirectionalLockEnabled = false //default false
        scroll.isPagingEnabled = false
        
        scroll.scrollIndicatorInsets = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 0)
        //add additional scroll area around content
        scroll.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        scroll.showsVerticalScrollIndicator = true
        scroll.indicatorStyle = .black
        scroll.bouncesZoom = true
        //如果正显示着键盘,拖动,则键盘撤回
        scroll.keyboardDismissMode = .onDrag
        scroll.flashScrollIndicators()
        
        scroll.delegate = self
        
        scroll.addSubview(theDayWorkoutLabel)
        theDayWorkoutLabel.anchor(top: scroll.topAnchor, left: scroll.leftAnchor, paddingTop: 5, paddingLeft: 20, width: screenWidth, height: 30)
        
        scroll.addSubview(theDayWorkoutButton)
        theDayWorkoutButton.anchor(top: theDayWorkoutLabel.bottomAnchor, left: scroll.leftAnchor, right: scroll.rightAnchor, paddingTop: 5, paddingLeft: 20, paddingRight: 20, width: 335, height: 220)
        
        scroll.addSubview(workoutLibraryLabel)
        workoutLibraryLabel.anchor(top: theDayWorkoutButton.bottomAnchor, left: scroll.leftAnchor, paddingTop: 5, paddingLeft: 20, width: screenWidth, height: 30)
        
        scroll.addSubview(collectionView)
        collectionView.anchor(top: workoutLibraryLabel.bottomAnchor, left: scroll.leftAnchor, paddingTop: 5, paddingLeft: 20, paddingRight: 20, width: 335, height: CGFloat(175 * numberOfItems / 2))
        
        return scroll
    }()
    
     override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        view.addSubview(scrollView)
        
        scrollView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)
        
    }
}

刚开始采用上面的代码在Xcode的simulator中运行,发现能拖动页面,但是立即反弹至顶部。找寻多家答案后,在Stack Overflow上找到了一个解决办法。

该答主说:A lot of the time the code is correct if you have followed a tutorial but what many beginners do not know is that the scrollView is NOT going to scroll normally through the simulator. It is suppose to scroll only when you press down on the mousepad and simultaneously scroll. Many Experienced XCode/Swift/Obj-C users are so use to doing this and so they do not know how it could possibly be overlooked by beginners.

也就是说scrollView代码块并没有问题,是因为通常来说scrollView不会在simulator中滚动,只有按下鼠标滚轮并同时滚动才能使scrollView滚动(mousepad是鼠标垫的意思吧)。但至于其中原因我也不太清楚,总之答主给出了一段解决该问题的代码,我进行了contentSize上的修改,使其能适配scrollView中collectionView cell数量的不同来自适应大小。

override func viewWillLayoutSubviews(){
        super.viewWillLayoutSubviews()
        let screenFrame = UIScreen.main.bounds
        let screenWidth = screenFrame.size.width
        
        scrollView.contentSize = CGSize(width: screenWidth, height: CGFloat(175 * numberOfItems / 2 + 280))
    }

下面给出苹果官方文档对func viewWillLayoutSubviews的解释:
When a view’ bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes before the view lays out its subviews. The default implementation of this method does nothing.

在这里给出我的理解,就是因为先加载了scrollView,再加载其子视图,因此通过这个函数使得再加载所有视图前先给出scrollView的contentSize,从而能顺利滚动。

发布了8 篇原创文章 · 获赞 2 · 访问量 1061

猜你喜欢

转载自blog.csdn.net/daiyucheng88/article/details/99562805