简体   繁体   English

分页滚动视图上的中心元素

[英]Center element on paginated scroll view

I am creating UIImageViews programatically and scroll between them as pagination 我正在以编程方式创建UIImageViews并在它们之间作为分页滚动

How can I center each image with auto-layout programatically? 如何以编程方式使用自动布局将每个图像居中?

// manual attempt 
viewDidLoad() {

    self.scroll.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height)

    let scrollWidth: CGFloat = self.scroll.frame.width

    let imgOne = UIImageView(frame: CGRect(x:65, y:120, width:180, height:300))
    let imgTwo = UIImageView(frame: CGRect(x:scrollWidth + 65, y:120, width:180, height:300))
    let imgThree = UIImageView(frame: CGRect(x:scrollWidth*2 + 65, y:120,width:180, height:300))

    imgOne.image = UIImage(named: "one.png")
    imgTwo.image = UIImage(named: "two.png")
    imgThree.image = UIImage(named: "three.png")

    imgOne.center = self.view.center // works
    imgTwo.center = ?? 
    imgThree.center = ??

    self.scroll.addSubview(imgOne)
    self.scroll.addSubview(imgTwo)
    self.scroll.addSubview(imgThree)

}

Use UICollectionView . 使用UICollectionView You can set pagingEnabled for paging between images and UICollectionViewScrollDirectionHorizontal in layout for horizontal direction. 您可以将pagingEnabled设置为在图像和UICollectionViewScrollDirectionHorizontal之间进行分页, UICollectionViewScrollDirectionHorizontal在水平方向上进行布局。 See good example of using it here . 在这里查看使用它的好例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM