gpt4 book ai didi

ios - 以编程方式在 UITableViewCell 内创建 UIScrollView - ScrollView 不滚动

转载 作者:行者123 更新时间:2023-11-30 11:36:04 25 4
gpt4 key购买 nike

我正在尝试在 UITableView Cell 中创建动态生成的 UIScrollViewUIScrollView 是使用 ScrollViewClass 中的构建函数构建的。该类采用 View 数组并将它们并排放置,然后返回一个 ScrollView,然后可以将其添加到单元格的 subview 中。 ScrollView 已添加且可见,但是 ScrollView 并未按预期水平滚动。所有的代码都可以在下面找到。

ScrollView类

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
}

最佳答案

您应该使用带有水平轴的 UIStackView,以便 ScrollView 可以正确获取 contentSize。

ScrollView类

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

您还应该在 cellForRowAt:

中对 scrollView 进行约束
    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

关于ios - 以编程方式在 UITableViewCell 内创建 UIScrollView - ScrollView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49757894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com