滚动浏览中的ios-all页面未加载自定义uiview

时间:2018-02-20 05:04:54

标签: ios swift swift3 uiview uiscrollview

自定义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个页面中填充它,但它只在第一页中加载。

如果我错过了什么,请帮助。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

您将多个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)

    }