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

本文详细解析了在Swift中使用UIScrollView时遇到的滚动问题,尤其是在Xcode模拟器中无法正常滚动的情况。通过调整contentSize和利用viewWillLayoutSubviews方法,实现了不同数量的collectionView cell自适应大小,确保了scrollView在模拟器中的正常滚动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,从而能顺利滚动。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值