简体   繁体   中英

Programmatically creating a UIScrollView inside of a UITableViewCell - ScrollView doesn't scroll

I am attempting to create a dynamically generated UIScrollView within a UITableView Cell . The UIScrollView is built using a build function within the ScrollViewClass . The class takes an array of views and positions them side by side, then returns a ScrollView which can then be added to the cell's subviews . The ScrollView is added and visible, however, the ScrollView does not scroll horizontally as intended. All of the code for this can be found below.

ScrollViewClass

import Foundation
import UIKit

class ScrollViewClass {
    let ArrayOfViews: [UIView]
    let scrollView = UIScrollView()

    init(ArrayOfViews: [UIView]) {
        self.ArrayOfViews = ArrayOfViews
    }

    func build() -> UIScrollView {
        scrollView.isScrollEnabled =  true
        scrollView.frame.size.width = UIScreen.main.bounds.width
        scrollView.frame.size.height = UIScreen.main.bounds.height
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.isPagingEnabled = true
        scrollView.contentSize = CGSize(width: 2000, height: 500)
        scrollView.showsHorizontalScrollIndicator = true

        for (index, views) in ArrayOfViews.enumerated() {
            scrollView.addSubview(views)
            views.frame.origin.x = CGFloat(index) * UIScreen.main.bounds.width / 2
            print(index)
        }

        return scrollView
    }
}

TableView CellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "CellID")
        cell.selectionStyle = UITableViewCellSelectionStyle.none;

        let view1 = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 500))
        let view2 = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 500))
        let view3 = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 500))

        view1.backgroundColor = UIColor.blue
        view2.backgroundColor = UIColor.green
        view3.backgroundColor = UIColor.red

        let ArrayOfViews = [view1, view2, view3]
        let scrollView = ScrollViewClass(ArrayOfViews: ArrayOfViews)
        cell.addSubview(scrollView.build())

        return cell
    }

You should use UIStackView with horizontal axis so the scrollview can get the contentSize properly.

ScrollViewClass

import Foundation
import UIKit

class ScrollViewClass {
    let ArrayOfViews: [UIView]
    let scrollView = UIScrollView()

    init(ArrayOfViews: [UIView]) {
        self.ArrayOfViews = ArrayOfViews
    }

    func build() -> UIScrollView {
        scrollView.isScrollEnabled =  true
        scrollView.frame.size.width = UIScreen.main.bounds.width
        scrollView.frame.size.height = UIScreen.main.bounds.height
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.isPagingEnabled = true
        scrollView.contentSize = CGSize(width: 2000, height: 500)
        scrollView.showsHorizontalScrollIndicator = true
        let stackView:UIStackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.alignment = .fill
        stackView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        for (index, views) in ArrayOfViews.enumerated() {
            stackView.addArrangedSubview(views)
            print(index)
        }
        scrollView.addSubview(stackView)  
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0).isActive = true 
        stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0).isActive = true
        stackView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
        stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0).isActive = true  
        return scrollView
    }
}

You should also give constraints to the scrollView in the cellForRowAt:

    let scrollView = ScrollViewClass(ArrayOfViews: ArrayOfViews)
    cell.addSubview(scrollView.build())
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: 0).isActive = true 
    scrollView.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: 0).isActive = true
    scrollView.topAnchor.constraint(equalTo: cell.topAnchor, constant: 0).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: cell.bottomAnchor, constant: 0).isActive = true  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM