[英]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.