UIScrollview+UITableView+UICollectionView问题总结

本文探讨了在iOS开发中,使用UIScrollView与UITableView时遇到的位置布局偏移问题,包括上移或下移,并提供了多种解决方案,如调整contentInsetAdjustmentBehavior、使用CATransaction、UIView动画设置等,同时分享了在预估行高、处理刷新闪屏及确保各分区至少有一个cell的高度设定等细节上的经验。

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

位置布局发生偏移(上移或下移)

if (@available(iOS 11.0, *)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //tableView.contentInsetAdjustmentBehavior = .never
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

//
        tableView.contentInset = UIEdgeInsets.zero
        tableView.scrollIndicatorInsets = tableView.contentInset

reloadData或reloadSections页面闪屏

//1、解决layer层闪屏问题
                CATransaction.begin()
                CATransaction.setCompletionBlock {
                    self.tableView.reloadSections(IndexSet.init(integer: sender.tag), with: .none)
                }
                CATransaction.commit()
//2、
                UIView.setAnimationsEnabled(false)
                self.tableView.reloadSections(IndexSet.init(integer: sender.tag), with: .none)
                UIView.setAnimationsEnabled(true)
//3、
                tableView.beginUpdates()
                self.tableView.reloadSections(IndexSet.init(integer: sender.tag), with: .none)
                tableView.endUpdates()
//4、
                UIView.performWithoutAnimation {
                    self.tableView.reloadSections(IndexSet.init(integer: sender.tag), with: .none)
                }

上拉加载更多数据,或刷新数据,页面位置会发生偏移,跳动,甚至是跳到了顶部

        tableView.estimatedRowHeight = 44
        tableView.estimatedSectionHeaderHeight = 44
        tableView.estimatedSectionFooterHeight = 30

如果你没有用到预估高度,可以关闭 “=0”,我是用的约束布局,高度自适应的在这里插入图片描述
无论你评论多长,高度都可以自适应。(这三行代码可以解决99%的问题)
上面提到的甚至是跳到了顶部,这个甚至是特殊业务需求的,像我这个评论页面就遇到了,去百度,去翻墙都没有找到类似案例的,当我点击收起刷新数据页面时,页面会回滚到顶部,可歌可泣!控制台打印

[Assert] Unable to determine new global row index for preReloadFirstVisibleRow (0)
我理解的是:每个分区都要有cell,没有的话他就回滚到顶部。
解决方案是:根据自己的业务需求,保证cell是0行也要返回一个高度为0的cell,保证分区至少有一个cell就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuwuFQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值