在Swift中嵌套UIScrollView

时间:2018-05-01 14:58:57

标签: ios swift uiscrollview

我有一个图像数组的滚动视图,它适用于图像向左和向右滚动但我无法向下滚动,而是我能够滚动我的tableview单元格...我需要能够向下滚动没有在视图上修复我的图像数组,有什么方法吗?

我使用的代码是:

    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)
    }

}

至于我的故事板,它看起来像:enter image description here

如果我将我的scrollview放在我的tableview中,我的代码就是     func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - &gt; UITableViewCell {         let cell:MainMenuRowSelectionTableViewCell = self.tableView.dequeueReusableCell(withIdentifier:“Cell”,for:indexPath)as! 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
}

和我的故事板

enter image description here

这样,我的滚动视图就不会出现了

2 个答案:

答案 0 :(得分:2)

  

我需要能够向下滚动,而不是在视图上修复我的图像数组

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

答案 1 :(得分:0)

是的,这可以做到。嵌套滚动视图的一个很好的参考是Apple Documentation那里也提供了示例代码。

简而言之,您可能需要创建:

  1. 填充视图并可以垂直滚动的UIScrollView
  2. 图片内的UIScrollView,可以横向滚动,
  3. 您的UITableView也位于第一个滚动视图中。
  4. enter image description here