UIScrollView和图像的页面控件。图片无法缩放到设备尺寸。

时间:2018-11-13 22:44:35

标签: xcode uiscrollview uiimage swift4 uipagecontrol

我无法将图像缩放到不同尺寸的设备。使用页面控制,我试图创建类似水平滚动相册的内容。我可以确定我没有为UIScrollView设置宽度和高度,因为图像只会在情节提要中显示为UIscrollView的宽度和高度。 UIscrollView在情节提要中没有任何限制,因为它需要动态并且符合图像数量。

This is an example of how it should look on all devices. This is on the iPhone 8 Plus

This is what happens on the iPad 12.9 inch. The concern is the image not going the full width of the screen and scaling the height and the page control dots which need to be in the center

这是我要执行的操作的代码:

    @IBOutlet weak var imageScrollView: UIScrollView!
    @IBOutlet weak var pageControl: UIPageControl!

    // Create array to hold images and frame for the images
    var buildingImages: [String] = []
    var frame = CGRect(x: 0, y: 0, width: 0, height: 0)

    // Get index for the number of pages for page control

    pageControl.numberOfPages=buildingImages.count
    for index in 0..<buildingImages.count{

        let xPosition=self.view.frame.width * CGFloat(index)
        frame.origin.x=xPosition

        frame.size=imageScrollView.frame.size

        // Initialize an image view and set it to an image, add image on top of scroll view
        let buildingImageView=UIImageView(frame: frame)
        buildingImageView.image=UIImage(named: buildingImages[index])

        buildingImageView.pin_setImage(from: URL(string: buildingImages[index]))

        self.imageScrollView.addSubview(buildingImageView)
    }
    /*
    Set up content size of scroll view (get size of scroll view by
    the amount of images in the scroll view
    */

    imageScrollView.contentSize=CGSize(width: (imageScrollView.frame.size.width*CGFloat(buildingImages.count)), height: imageScrollView.frame.size.height)
    imageScrollView.delegate=self

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    var pageNumber = scrollView.contentOffset.x/scrollView.frame.size.width
    pageControl.currentPage=Int(pageNumber)
}

0 个答案:

没有答案