首页 > 解决方案 > 在 Swift 中嵌套 UIScrollView

问题描述

我有一个图像数组的滚动视图,它适用于图像左右滚动,但我无法向下滚动,而是能够滚动我的表格视图单元格......我需要能够向下滚动并且没有我的固定在视图上的图像数组,有什么办法吗?

我使用的代码是:

    override func viewDidLoad() {
    super.viewDidLoad()
    scrollView.frame = view.frame
    imageArray = [UIImage(named:"adsImage3")!,UIImage(named:"adsImage2")!,UIImage(named:"adsImage")!]
    navigationController?.navigationBar.barTintColor = UIColor.white

    for i in 0..<imageArray.count {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.clipsToBounds = true
        imageView.image = imageArray[i]
        let xPosition = self.view.frame.width * CGFloat(i)
        imageView.frame = CGRect(x: xPosition, y: 0, width: self.scrollView.frame.width, height: 380)

        scrollView.contentSize.width = scrollView.frame.width * CGFloat(i + 1)
        scrollView.addSubview(imageView)
    }

}

至于我的故事板,它看起来像:在此处输入图像描述

如果我将滚动视图放在我的表格视图中,我的代码将是 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: MainMenuRowSelectionTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for : indexPath) 一样!MainMenuRowSelectionTableViewCell

    var cellColors = ["#FFB3B3","#FFFFFF"]
    cell.contentView.backgroundColor = UIColor(hexString: cellColors[indexPath.row % cellColors.count])

    imageArray = [UIImage(named:"adsImage3")!,UIImage(named:"adsImage2")!,UIImage(named:"adsImage")!]

    for i in 0..<imageArray.count {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.clipsToBounds = true
        imageView.image = imageArray[i]
    }
    return cell
}

和我的故事板

在此处输入图像描述

这样,我的滚动视图根本不会出现

标签: iosswiftuiscrollview

解决方案


我需要能够向下滚动并且不将我的图像数组固定在视图上

表格视图垂直滚动视图。您应该将图像的水平滚动视图转换为表格视图(即它的tableHeaderView)的标题视图,并使表格视图占据整个屏幕。现在您可以滚动表格,并将图像的滚动视图向上滚动到屏幕之外。


推荐阅读