【问题标题】:UIScrollView not showing up in the viewUIScrollView 没有显示在视图中
【发布时间】:2019-10-08 20:51:11
【问题描述】:

我在CollectionViewCell 中实现UIScrollView。我有一个滚动视图应该显示的自定义视图,因此我在CollectionViewCell 中执行以下程序。我以编程方式创建了所有内容,下面是我的代码:

struct ShotsCollections {
    let title: String?
}

class ShotsMainView: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)

        setupViews()

        containerScrollView.contentSize.width = frame.width * CGFloat(shotsData.count)

        shotsData = [ShotsCollections.init(title: "squad"), ShotsCollections.init(title: "genral")]
        var i = 0
        for data in shotsData {

            let customview = ShotsMediaView(frame: CGRect(x: containerScrollView.frame.width * CGFloat(i), y: 0, width: containerScrollView.frame.width, height: containerScrollView.frame.height))

            containerScrollView.addSubview(customview)
            i += 1
        }

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var shotsData = [ShotsCollections]()

    var containerScrollView: UIScrollView = {
        let instance = UIScrollView()
        instance.isScrollEnabled = true
        instance.bounces = true
        instance.backgroundColor = blueColor
        return instance
    }()


    private func setupViews() { //These are constraints by using TinyConstraints
        addSubview(containerScrollView)
        containerScrollView.topToSuperview()
        containerScrollView.bottomToSuperview()
        containerScrollView.rightToSuperview()
        containerScrollView.leftToSuperview()
    }
}

现在的问题是,当显示滚动视图时,其中的内容却没有。我在打印滚动视图的contentSizeframe 时,它显示为0。但是如果我检查调试视图层次结构,滚动视图包含2 个带有特定框架的视图。

我不确定出了什么问题。任何帮助表示赞赏。

【问题讨论】:

  • 如果您的问题得到解答,您必须通过接受答案来结束问题。
  • @kerry Hey Buddy 尝试了您的解决方案,但正如我所提到的,约束并没有完全说服我,也没有锻炼。主要是将滚动视图的框架设置为等于 supreview 并将数组数据分配到内容大小之上。这本身将创建可滚动的内容。但我肯定会赞成你给我的所有及时帮助。干杯

标签: ios swift uiview uiscrollview uicollectionview


【解决方案1】:

当您在containerScrollView 中添加customView 时,您并未在customViewcontainerScrollView 之间设置约束。

添加这些约束,您将能够看到您的customViews,因为您的customView 有一定的高度。此外,当您添加更多视图时,您需要删除最后添加的视图的底部约束,并使用最新添加的视图为containerScrollView 创建底部约束。

【讨论】:

  • 好吧,我不太明白添加约束。我已经为滚动视图提供了框架。实际上,如果我在情节提要上使用滚动视图执行相同的代码并其余全部编程,则它可以完全正常工作。一旦我转向对滚动视图进行编程,一切都会向南。
  • 试着理解答案。如果您以编程方式创建所有内容,则您也需要以编程方式添加约束。 NSLayoutConstraints.activate()。由于您在添加时没有在 customView 中添加约束,因此您无法看到它,并且它的高度没有被转换为 scrollView 高度
  • 该死!我明白你在说什么,现在完全明白你的意思了。我的坏 _facepalms_.. 那么,如果我水平滚动,我应该替换左右约束,对吧?
  • 是的。如果要在右侧添加新视图,则需要替换最后一个视图的右约束并使用新视图创建一个链。然后将新视图的右约束添加到superview的右边,从而结束链。
  • 那么在这种情况下,我需要唯一标识我的视图吗?你能帮我一些链接吗?
【解决方案2】:

我为您的用例创建了一个示例应用。我在下面粘贴代码和生成的屏幕截图。希望这是您正在寻找的功能。我建议您将其粘贴到一个新项目中并调整代码,直到您满意为止。我添加了 cmets 以使其清楚。

视图控制器

import UIKit

class ViewController: UIViewController {

    // Initialize dummy data array with numbers 0 to 9
    var data: [Int] = Array(0..<10)

    override func loadView() {
        super.loadView()
        // Add collection view programmatically
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.register(ShotsMainView.self, forCellWithReuseIdentifier: ShotsMainView.identifier)
        self.view.addSubview(collectionView)
        NSLayoutConstraint.activate([
            self.view.topAnchor.constraint(equalTo: collectionView.topAnchor),
            self.view.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor),
            self.view.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor),
            self.view.trailingAnchor.constraint(equalTo: collectionView.trailingAnchor),
        ])
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.backgroundColor = UIColor.white
        self.view.addSubview(collectionView)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.view.backgroundColor = UIColor.white
    }
 }   

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ShotsMainView.identifier, for: indexPath) as! ShotsMainView
        return cell
    }
}

extension ViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        // The cell dimensions are set from here
        return CGSize(width: collectionView.frame.size.width, height: 100.0)
    }
}

ShotsMainView 这是集合视图单元格

import UIKit

class ShotsMainView: UICollectionViewCell {

    static var identifier = "Cell"
    weak var textLabel: UILabel!

    override init(frame: CGRect) {
        // Initialize with zero frame
        super.init(frame: frame)
        // Add the scrollview and the corresponding constraints
        let containerScrollView = UIScrollView(frame: .zero)
        containerScrollView.isScrollEnabled = true
        containerScrollView.bounces = true
        containerScrollView.backgroundColor = UIColor.blue
        containerScrollView.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(containerScrollView)
        NSLayoutConstraint.activate([
            self.topAnchor.constraint(equalTo: containerScrollView.topAnchor),
            self.bottomAnchor.constraint(equalTo: containerScrollView.bottomAnchor),
            self.leadingAnchor.constraint(equalTo: containerScrollView.leadingAnchor),
            self.trailingAnchor.constraint(equalTo: containerScrollView.trailingAnchor)
        ])

        // Add the stack view that will hold the individual items that
        // in each row that need to be scrolled horrizontally
        let stackView = UIStackView(frame: .zero)
        stackView.distribution = .fill
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.axis = .horizontal
        containerScrollView.addSubview(stackView)
        stackView.backgroundColor = UIColor.magenta

        NSLayoutConstraint.activate([
            containerScrollView.leadingAnchor.constraint(equalTo: stackView.leadingAnchor),
            containerScrollView.trailingAnchor.constraint(equalTo: stackView.trailingAnchor),
            containerScrollView.topAnchor.constraint(equalTo: stackView.topAnchor),
            containerScrollView.bottomAnchor.constraint(equalTo: stackView.bottomAnchor)
        ])

        // Add individual items (Labels in this case).
        for i in 0..<10 {
            let label = UILabel(frame: .zero)
            label.translatesAutoresizingMaskIntoConstraints = false
            stackView.addArrangedSubview(label)
            label.text = "\(i)"
            label.font = UIFont(name: "System", size: 20.0)
            label.textColor = UIColor.white
            label.backgroundColor = UIColor.purple
            label.layer.masksToBounds = false
            label.layer.borderColor = UIColor.white.cgColor
            label.layer.borderWidth = 1.0
            label.textAlignment = .center


            NSLayoutConstraint.activate([
                label.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 1.0, constant: 0.0),
                label.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.2, constant: 0.0)
            ])
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

截图

【讨论】:

  • 感谢您的回复。我在这里提出了一个详细的问题。可以看看吗?
  • 我检查了你的代码,我认为你没有正确设置约束。你已经为容器滚动视图添加了约束,但我没有看到你设置 containerScrollView.translatesAutoresizingMaskIntoConstraints = false,这不会得到你正在寻找的东西。您也没有使用堆栈视图,并且在 for 循环中添加的视图的框架是明确设置的,而不是使用约束。我要求您使用上面的代码设置一个示例项目,了解正在发生的事情并逐步编辑代码以匹配您的场景。
  • 嘿伙计!非常感谢。我实际上只是用滚动视图解决了这个问题。我其实是指着这个问题。你能帮我吗stackoverflow.com/questions/56339775/…
  • 嘿伙计,你能帮帮我吗:stackoverflow.com/questions/56656783/…
猜你喜欢
  • 2017-03-09
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 2016-02-04
相关资源
最近更新 更多