UIScrollView初始化时的内容偏移量

问题描述 投票:0回答:1

[我们目前正在为我们的iOS应用程序使用较旧的代码库,并且遇到了一个怪异的错误,其中UIScrollViews分页在初始化时不匹配,仅在用户选择按钮以更改视图时才匹配。

每个ScrollView内嵌了三张幻灯片。我们像这样初始化ScrollView:

override init(frame: CGRect) {
    super.init(frame: frame)
    self.commonInit()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.commonInit()
}

private func commonInit() {

    Bundle.main.loadNibNamed("DIScrollView", owner: self, options: nil)
    contentView.frame = self.bounds
    addSubview(contentView)
    contentView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
    contentView.layer.borderColor = UIColor.white.cgColor
    contentView.layer.borderWidth = 2.0

    scrollView.delegate = self
    setUpScrollViewer()
}

您可以看到我们调用了ScrollView的设置,就像这样:

public func setUpScrollViewer() {

    let slides = self.createSlides()
    let defaultIndex = 1
    scrollView.Initialize(slides: slides, scrollToIndex: defaultIndex)


    pageControl.numberOfPages = slides.count
    pageControl.currentPage = defaultIndex
}

现在所有内容都可用于每张幻灯片,我们想使用ScrollView扩展名来处理内容:

extension UIScrollView {

//this function adds slides to the scrollview and constraints to the subviews (slides)
//to ensure the subviews are properly sized
func Initialize(slides:[UIView], scrollToIndex:Int) {

    //Take second slide to base size from
    let frameWidth = slides[1].frame.size.width

    self.contentSize = CGSize(width: frameWidth * CGFloat(slides.count), height: 1)

    for i in 0 ..< slides.count {

        //turn off auto contstraints. We will be setting our own
        slides[i].translatesAutoresizingMaskIntoConstraints = false

        self.addSubview(slides[i])

        //pin the slide to the scrollviewers edges
        if i == slides.startIndex {
            slides[i].leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
        } else { //pin each subsequent slides leading edge to the previous slides trailing anchor
            slides[i].leadingAnchor.constraint(equalTo: slides[i - 1].trailingAnchor).isActive = true
        }

        slides[i].topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        slides[i].widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
        slides[i].heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
    }

    //the last slides trailing needs to be pinned to the scrollviewers trailing.
    slides.last?.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true


    self.scrollRectToVisible(CGRect(x: frameWidth * CGFloat(scrollToIndex), y: 0, width: frameWidth, height: 1), animated: false)

}

}

我已经尝试过手动设置contentOffset,并且似乎在初始化方面没有任何调整。如果用户选择该按钮,则将其隐藏然后取消隐藏以正确显示它,而无需逻辑调整。给我留下这个问题的印象是在初始化上。

摘要:当加载主视图时,当我需要专注于第二张幻灯片时,scrollView将向我显示索引中的第一张幻灯片。但是,如果用户隐藏然后取消隐藏scrollView,它将按预期工作。

我如何获得UIScrollView来实际加载并初始化更新scrollView以显示第二张幻灯片而不在第一张幻灯片上初始化?

ios swift uiscrollview
1个回答
0
投票

我的猜测是,所有这些代码都会在布局系统对视图进行定位之前运行,并且第一张幻灯片的框架是默认的0 x 0尺寸。当应用返回此视图时,自动布局会计算出此幻灯片的大小,因此计算有效。

进入布局循环以滚动到布局后的正确位置。也许重写viewDidLayoutSubviews()来检查它是否在初始布局中,然后设置滚动位置。

© www.soinside.com 2019 - 2024. All rights reserved.