首页 > 解决方案 > 迅速。设置 UIScrollView 的 contentInset 不起作用

问题描述

我有一个 UIView,上面有一个 UIScrollView。

let curtainView = UIScrollView ()
curtainView.snp.makeConstraints {make in
            make.edges.equalToSuperview ()
}

还有 UIView。

let someView = UIView ()
someView.snp.makeConstraints {make in
        make.top.equalToSuperview (). offset (10)
        make.leading.trailing.equalToSuperview (). inset (10)
}

Objective: 根据 UIView 的可见性为 UIScrollView 设置 contentInset。UIView 的可见性通过 RxSwift 改变。我正在做以下事情

override func layoutSubviews () {
        super.layoutSubviews ()
        curtainView.contentInset.top = someView.isHidden? self.frame.minY: someView.frame.maxY
    }

layoutSubviews 方法被多次调用:

1.curtainView.contentInset.top == 0
2.curtainView.contentInset.top == 0
3.curtainView.contentInset.top == 30

然而,视觉上 someView 的 contentInset 保持不变。只有在滑动之后,窗帘才会设置正确的缩进。我无权添加图片,请在第三方资源上查看:https ://youtu.be/LecHmLd9HP4

标签: uiscrollviewrx-swift

解决方案


推荐阅读