简体   繁体   English

使用故事板将集合视图布局设置为组合布局

[英]Setting collection view layout to compositional layout using storyboard

How would I be able to change collection view flow layout to a compositional layout from storyboard.我如何能够将集合视图流布局从故事板更改为组合布局。 So I have a TabBarController class that contains 3 nav controllers that I init from storyboard looks like this.所以我有一个 TabBarController 类,其中包含我从故事板初始化的 3 个导航控制器,如下所示。

//Storyboard is an enum that contains strings IDs that belong to controllers from main storyboard.
private func createNewViewController(storyboardIdentifier: Storyboard.Controller) -> UIViewController {
    let controller = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: storyboardIdentifier.indentifer)

    return controller
}

This is how I add the controllers to the tab bar controller.这就是我将控制器添加到标签栏控制器的方式。

viewControllers = [
        SearchCollectionVC(),
        //createNewViewController(storyboardIdentifier: .searchNav),
        createNewViewController(storyboardIdentifier: .todayNav),
        createNewViewController(storyboardIdentifier: .appsNav)

    ]

If I try to add my SearchCollectionVC with the commented line my app crashes.如果我尝试添加带有注释行的 SearchCollectionVC,我的应用程序就会崩溃。 It executes the line fatalError() in the required init from the SearchCollectionVC.它在来自 SearchCollectionVC 的所需 init 中执行 fatalError() 行。 This is how I create the layout.这就是我创建布局的方式。

init() {
    super.init(collectionViewLayout: SearchCollectionVC.createCollectionLayout())
}


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

static func createCollectionLayout() -> UICollectionViewCompositionalLayout {
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
    let item = NSCollectionLayoutItem(layoutSize: itemSize)

    let grupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(250))
    let group = NSCollectionLayoutGroup.horizontal(layoutSize: grupSize, subitems: [item])

    let section = NSCollectionLayoutSection(group: group)
    section.interGroupSpacing = 20

    let layout = UICollectionViewCompositionalLayout(section: section)

    return layout
}

Now I tried doing this in the SearchCollectionVC viewDidLoad().现在我尝试在 SearchCollectionVC viewDidLoad() 中执行此操作。

collectionView.collectionViewLayout = SearchCollectionVC.createCollectionLayout()

This works with the comment line and without the init.这适用于注释行而无需 init。 The thing is when I rotate the screen this happens.问题是当我旋转屏幕时会发生这种情况。

在此处输入图片说明

This doesn't happen when I add the controller using SearchCollectionVC() and when using the init.当我使用 SearchCollectionVC() 添加控制器和使用 init.d 时,不会发生这种情况。 Now, I don't know why this is happening.现在,我不知道为什么会发生这种情况。 Can someone please let me know how would I be able to init a storyboard controller and change the layout to the compositional layout I created, without having the collection view getting cut in half.Thanks有人可以让我知道我如何能够初始化故事板控制器并将布局更改为我创建的组合布局,而不会将集合视图切成两半。谢谢

Just found out how to do.刚刚知道怎么做。 Had to create a new collection view like this.不得不像这样创建一个新的集合视图。

 collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: SearchCollectionVC.createCollectionLayout())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有组合布局的集合视图不适用于 header 视图 - Collection view with Compositional Layout not working for header view 集合视图组合布局 - 动态布局 - Collection View Compositional Layouts - Layout on the fly 具有估计高度的集合视图组合布局不起作用 - Collection View Compositional Layout with estimated height not working "如何使用集合视图组合布局使集合视图内的单元格居中" - How to center cells inside collection view using collection view compositional layout 如何在 iOS 上隐藏 header 13 集合视图组合布局 - How hide header on iOS 13 Collection View Compositional Layout 如何防止自动聚焦在集合视图组合布局 swift - How to prevent auto focus on collection view compositional layout swift 集合视图单元格组合布局之间不需要的空间 - Unwanted space between Collection View cells Compositional Layout 我认为集合视图拖放+组合布局不能一起工作是否正确? - Am I correct in thinking collection view drag and drop + compositional layout just don't work together? 故事板视图(iphone 4/5/6)布局影响集合单元大小(以编程方式设置) - Storyboard view(iphone 4/5/6) layout effecting collection cell size (programmatically set) 如何使用这样的图像/图标/视图偏移组合布局 - How can I offset a Compositional Layout with an image / icon / view like this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM