UIScrollView水平内容大小调整并添加UILabels

问题描述 投票:0回答:1

我有一个问题,现在我试图解决该问题已有好几天,但遗憾的是它不起作用。我正在使用UIScrollView进行水平分页。它实际上在工作,但是:

问题1:无论我做什么,即将出现的图像已经显示在当前幻灯片中。

最重要的是,我还想拥有属于所显示图像的不同标签。这意味着每个图像都有一个来自数组的唯一标签。分页时,应在UIScrollView的Image下显示正确的Label。

我附上代码,一个屏幕快照,现在看起来像一个例子,我想如何拥有它。该示例的左右按钮不必一定存在。如果很容易,请随时告诉我如何执行此操作。 :)

已经非常感谢您的帮助人员!


class HomeViewController: UIViewController, UIScrollViewDelegate {


    //Mark: Properties



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






    var images: [String] = ["Project1", "Project2", "Project3", "Project4"]
    var frame = CGRect(x:0, y:0, width:0, height:0)

    let titles: [String] = ["Get High", "Get Tipsy", "Get Trippy", "Get fucked"]





    override func viewDidLoad() {
        super.viewDidLoad()

      // Specify how many pages it has = to page control numbers

        pageControl.numberOfPages = images.count
        for index in 0..<images.count {
            frame.origin.x = scrollView.frame.size.width * CGFloat(index) // width x amount of pages
            frame.size = scrollView.frame.size


            // initialize image view "add on top of scroll view"

            let imgView = UIImageView(frame: frame)
            imgView.image = UIImage(named: images[index])
            self.scrollView.addSubview(imgView)


        }

        // Setting the size of the scroll view

        scrollView.contentSize = CGSize(width: (scrollView.frame.size.width * CGFloat(images.count)), height: scrollView.frame.size.height)

        scrollView.delegate = self

        }


        // Scrollview Method
        // --> Set new image and new page Control position

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        var pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width // what actual page we are on
        pageControl.currentPage = Int(pageNumber)
    }

        }














UIScrollView right now

something like this would be nice :)

ios swift xcode uiscrollview
1个回答
0
投票

您可以通过collection-view实现相同的目的。无论如何,您是否尝试过启用分页?

© www.soinside.com 2019 - 2024. All rights reserved.