ios-scrollview 中的所有页面未加载自定义 uiview

ios-all pages in scrollview not loading with custom uiview

自定义 uiview 仅在第一页加载,这是我正在处理的代码:

import UIKit

class MainViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var scrollView: UIScrollView!

var customframe: CGRect!

override func viewDidLoad() {
    super.viewDidLoad()
    customframe = CGRect(x: 0.0, y: 0.0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)

    scrollView.layer.masksToBounds = true
    scrollView.layer.cornerRadius = scrollView.frame.height/4
}
override func viewWillAppear(_ animated: Bool) {
    self.loadScrollView()
}
func loadScrollView() {
    scrollView.backgroundColor = UIColor.black
    scrollView.delegate = self
    scrollView.isPagingEnabled = true
    scrollView.contentSize = CGSize(width: scrollView.frame.size.width * 5, height: scrollView.frame.size.height)
    scrollView.showsHorizontalScrollIndicator = true
    for i in 0..<5 {
        print(self.scrollView.frame.size.width)

        let progressChartView = ProgressChartView(frame: customframe)//initialise cgdraw
        progressChartView.setNeedsDisplay()
        self.scrollView.addSubview(progressChartView)

    }
}

这里的 progressChartView 是 UIView 子类的实例,我想在 scrollView 的所有 5 个页面中填充它,但它只在第一页加载。

如果我遗漏了什么,请帮忙。 提前致谢。

您将多个 ProgressChartView 放在同一个位置

替换您的代码

 for i in 0..<5 {
        print(self.scrollView.frame.size.width)

        let progressChartView = ProgressChartView(frame: customframe)//initialise cgdraw
        progressChartView.setNeedsDisplay()
        self.scrollView.addSubview(progressChartView)

    }

for i in 0..<5 {
        print(self.scrollView.frame.size.width)
        let frame = CGRect(x: i *  self.scrollView.frame.origin.x,y:0,width:self.scrollView.frame.size.width,height:self.scrollView.frame.size.height)
        let progressChartView = ProgressChartView(frame: frame)//initialise cgdraw
        progressChartView.setNeedsDisplay()
        self.scrollView.addSubview(progressChartView)

    }