在滚动视图中修复图像的错误位置

时间:2018-04-08 19:34:53

标签: swift scrollview

昨天我决定创建一个入门屏幕,但是我在滚动视图中显示图像时遇到了困难。当然第一个插图没问题,但第二个和第三个图像并不位于屏幕中间。

override func viewDidLoad() {
    super.viewDidLoad()
    abc = [a,b,c]
    // SCROLL VIEW
    OBGScrollView.isPagingEnabled = true
    OBGScrollView.contentSize = CGSize(width: self.view.bounds.width * CGFloat(abc.count), height: 287)
    OBGScrollView.showsHorizontalScrollIndicator = false
    OBGScrollView.delegate = self
    // PAGE CONTROL
    OBGPageControl.transform = CGAffineTransform(scaleX: 1, y: 1)
    Illustrations()
}

func Illustrations() {
    for (index, illustrations) in abc.enumerated() {
        if let abcView = Bundle.main.loadNibNamed("sView", owner: self, options: nil)?.first as? sUIView {
            abcView.illustration.image = UIImage(named: illustrations["image"]!)
            abcView.title.text = illustrations["title"]
            abcView.desc.text = illustrations["description"]
            OBGScrollView.addSubview(abcView)
            abcView.frame.size.width = self.view.bounds.size.width
            abcView.frame.origin.x = CGFloat(index) * self.view.bounds.size.width
        }
    }
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let page = scrollView.contentOffset.x / scrollView.frame.size.width
    OBGPageControl.currentPage = Int(page)
}

Image

1 个答案:

答案 0 :(得分:0)

在第42,43行中,您应该在将abcView添加到子视图之前设置其宽度和x位置。所以只需在这两行之后移动第41行。