ios - UICollectionView 未显示以查看

标签 ios swift uicollectionview constraints nslayoutconstraint

我已经被这个问题困了将近一个星期了。我什至恢复了我的代码并编写了与某些教程相同的代码,但我似乎无法让我的 UICollectionView 终生显示在 View 中。我的 View Controller 中还有很多其他代码,所以我将显示属于 UICollectionView 的代码。这是我到目前为止所拥有的:

View Controller :

class UARTModuleViewController: UIViewController, CBPeripheralManagerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupMenuBar()
    }

    let menuBar: MenuBar = {
        let mb = MenuBar()
        return mb
    }()

    private func setupMenuBar() {
        view.addSubview(menuBar)
        let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":menuBar])
        let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0(100)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":menuBar])
        view.addConstraints(horizontalConstraint)
        view.addConstraints(verticalConstraint)
    }
}

MenuBar.swift:

class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    lazy var collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.red
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

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

        self.addSubview(collectionView)
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")

        //took out constraints here just to make the warning mentioned below easier to follow.
    }

我确实实现了我的协议(protocol),但我没有添加它们只是为了节省我发布的代码量。 UARTModuleViewController 加载后,我确实收到此警告:

[LayoutConstraints] Unable to simultaneously satisfy constraints.

我试过推理它,但似乎无法得出解决方案。我也有一个单元格类,但认为没有必要包含它,因为我无法显示 UICollectionView。我唯一可以添加的另一件事是我在 Storyboard中添加的 View 中间有一个 UIImageView 并像这样施加约束:enter image description here

有人对我可能做错的地方有任何建议吗?在此先感谢您提供的任何帮助或建议。

最佳答案

根据评论:

  • 首先,每次在代码中使用约束时,都必须为要添加约束的 View 设置translatesAutoresizingMaskIntoConstraints = false
  • 您并不是要让 Collection View 填充整个 MenuBar 空间。
  • setupMenuBar 中应用于 MenuBar 的约束不足以确定 View 的大小和位置。

您应该应用这些更改来告诉 MenuBar 填充宽度,高度为 60 pt 并定位在屏幕底部:

private func setupMenuBar() {
    view.addSubview(menuBar)
    menuBar.translatesAutoresizingMaskIntoConstraints = false // this will make your constraint working
    menuBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true // every constraint must be enabled.
    menuBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
    menuBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    menuBar.heightAnchor.constraint(equalToConstant: 60).isActive = true
}

要告诉 Collection View 填充整个 MenuBar View ,请在 MenuBar init(frame:) 方法中执行此操作:

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

    self.addSubview(collectionView)
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")

    collectionView.translatesAutoresizingMaskIntoConstraints = false        
    collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
    collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
    collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
    collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true

    //took out constraints here just to make the warning mentioned below easier to follow.
}

正如您在我的代码中看到的,我通常使用“ anchor ”方法来应用约束,因为它比视觉格式更容易。

关于ios - UICollectionView 未显示以查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53453124/

相关文章:

ios - AFNetworking - 除了 for 循环中的最终请求外,没有任何响应

ios - 具有 anchor 样式自动布局约束的 UIButtons 未显示

ios - "Google/Analytics.h file not found"将 pod 更新到 GoogleAnalytics 后

ios - UIScrollView 中的 UICollectionView -> 滚动顺序

ios - UIButton 在 collectionView 单元格中不起作用

ios - 使用 NSBlockOperation 和 EKEventStore 时主线程卡住

ios - 将 PFQuery 解析为数组 objective-c

ios - 未调用 TableView 的 cellForRowAtIndexPath?

ios - 找不到适合 iPhone5 架构的图片

swift - 删除 UICollectionView 单元格之间的空间