gpt4 book ai didi

ios - UICollectionView 项目按其部分位置偏移

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

我已经实现了 UICollectionView 来将 WKWebview 渲染为 UICollectionViewCell 的 contentView subview ,并将 UIPageControl 绑定(bind)到它。

Note: Just for demoing the problem while rendering, I have made the size of the cell width as 100

问题是第一个 Webview( Collection View 第一部分中的单元格)按预期显示,但第二个单元格显示有单元格宽度的偏移。

UICollectionViewDelegate 重写方法

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1;
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// set this width for just showing screenshot for this question
return CGSize(100, height: view.frame.height-view.safeAreaInsets.top);
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0;
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
self.webviewsPageControl.currentPage = indexPath.section;
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
self.webviewsPageControl.numberOfPages = self.webviews?.count ?? 0;
return self.webviews?.count ?? 0;
}

enter image description here

为了便于识别,我将 UICollectionViewCell 背景设置为红色。

所以问题是,如何删除偏移量,以便 webview 可以并排渲染。为什么会出现这种行为。

提前致谢。

最佳答案

您需要在流布局上设置minimumLineSpacing = 0,例如

let layout = UICollectionViewFlowLayout();
layout.minimumLineSpacing = 0
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

关于ios - UICollectionView 项目按其部分位置偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49740684/

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