William Loke
William Loke

Reputation: 377

Nest a UIScrollView in Swift

I have a scrollview of an array of images, it works for the images to scroll left and right but I cannot scroll down, instead I am able to scroll my tableview cells...I need to be able to scroll downwards and not have my array of images fixed on the view, is there any ways?

the codes i used are :

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

}

as for my storyboard, it looks like:enter image description here

if I were to put my scrollview in my tableview, my code would be func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> 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
}

and my storyboard

enter image description here

this way,my scroll view wouldnt appear at all

Upvotes: 2

Views: 735

Answers (2)

matt
matt

Reputation: 534958

I need to be able to scroll downwards and not have my array of images fixed on the view

A table view is a vertical scroll view. You should turn your horizontal scroll view of images into the header view for the table view (i.e. its tableHeaderView), and make the table view occupy the whole screen. Now you can scroll the table and you'll scroll the scroll view of images up out of the screen.

Upvotes: 2

picciano
picciano

Reputation: 22701

Yes, this can be done. A good reference for nested scroll views is Apple Documentation There is sample code available there as well.

In short, you will likely need to create:

  1. A UIScrollView that fills the view and can scroll vertically,
  2. A UIScrollView within that for the images and can scroll horizontally, and
  3. Your UITableView also placed within the first scroll view.

enter image description here

Upvotes: 0

Related Questions