2
A
回答
1
您可以通过一些自定义UICollectionViewLayout
来实现UICollectionView
外观。补充意见可以帮助背景!
我可以建议SquareMosaicLayout,可以自定义,看起来像你提供的图像。屏幕截图显示,实现所需外观非常简单。它只是缺乏细胞之间的边界,但你可以轻松地自己做。
P.S.我已经将您的布局添加到存储库,因此您可以下载并运行该项目。使用SquareMosaicLayout
全码:
import UIKit
import SquareMosaicLayout
final class Hungcao: UIViewController, UICollectionViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
let collection = UICollectionView(frame: .zero, collectionViewLayout: HungcaoLayout())
collection.backgroundColor = UIColor.white
collection.contentInset = UIEdgeInsets(top: 60.0, left: 20.0, bottom: 10.0, right: 20.0)
collection.register(UICollectionViewCell.self)
collection.register(UICollectionReusableView.self, identifier: SquareMosaicLayoutSectionBacker, kind: SquareMosaicLayoutSectionBacker)
collection.register(UICollectionReusableView.self, identifier: SquareMosaicLayoutSectionHeader, kind: SquareMosaicLayoutSectionHeader)
collection.dataSource = self
self.title = "Hungcao"
self.view = collection
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: UICollectionViewCell = collectionView.dequeueCell(indexPath: indexPath)
cell.contentView.backgroundColor = UIColor.clear
let label = UILabel()
label.text = "Cell \(indexPath.row)"
label.font = UIFont.systemFont(ofSize: 20)
label.textAlignment = .center
label.frame = cell.contentView.bounds
cell.contentView.addSubview(label)
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case SquareMosaicLayoutSectionBacker:
let view: UICollectionReusableView = collectionView.dequeueSupplementary(kind, indexPath: indexPath, kind: kind)
view.layer.backgroundColor = UIColor.groupTableViewBackground.cgColor
view.layer.cornerRadius = 10.0
view.layer.borderWidth = 2.0
view.layer.borderColor = UIColor.black.cgColor
return view
case SquareMosaicLayoutSectionHeader:
let view: UICollectionReusableView = collectionView.dequeueSupplementary(kind, indexPath: indexPath, kind: kind)
view.layer.backgroundColor = UIColor.clear.cgColor
let label = UILabel()
label.text = "header"
label.font = UIFont.systemFont(ofSize: 20)
label.textAlignment = .center
label.frame = view.bounds
view.addSubview(label)
return view
default:
fatalError()
}
}
}
final class HungcaoLayout: SquareMosaicLayout, SquareMosaicDataSource {
convenience init() {
self.init(direction: SquareMosaicDirection.vertical)
self.dataSource = self
}
func layoutPattern(for section: Int) -> SquareMosaicPattern {
return HungcaoPattern()
}
func layoutSupplementaryBackerRequired(for section: Int) -> Bool {
return true
}
func layoutSeparatorBetweenSections() -> CGFloat {
return 60.0
}
func layoutSupplementaryHeader(for section: Int) -> SquareMosaicSupplementary? {
return HungcaoSupplementary()
}
}
final class HungcaoSupplementary: SquareMosaicSupplementary {
func supplementaryFrame(for origin: CGFloat, side: CGFloat) -> CGRect {
return CGRect(x: 0, y: origin, width: side, height: 60)
}
}
final class HungcaoPattern: SquareMosaicPattern {
func patternBlocks() -> [SquareMosaicBlock] {
return [HungcaoBlock()]
}
}
public class HungcaoBlock: SquareMosaicBlock {
public func blockFrames() -> Int {
return 4
}
public func blockFrames(origin: CGFloat, side: CGFloat) -> [CGRect] {
let width = side/2.0
var frames = [CGRect]()
frames.append(CGRect(x: 0, y: origin, width: width, height: 80))
frames.append(CGRect(x: width, y: origin, width: width, height: 80))
frames.append(CGRect(x: 0, y: origin + 80, width: width, height: 80))
frames.append(CGRect(x: width, y: origin + 80, width: width, height: 80))
return frames
}
}
// MARK: - UICollectionViewCell
extension UICollectionView {
func dequeueCell<T:UICollectionViewCell>(_ identifier: String? = nil, indexPath: IndexPath) -> T {
let identifier = identifier ?? String(describing: T.self)
let cell = self.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as? T
switch cell {
case .some(let unwrapped): return unwrapped
default: fatalError("Unable to dequeue" + T.self.description())
}
}
func register<T:UICollectionViewCell>(_ type: T.Type, identifier: String? = nil) {
let identifier = identifier ?? String(describing: T.self)
register(type, forCellWithReuseIdentifier: identifier)
}
}
// MARK: - UICollectionReusableView
extension UICollectionView {
func dequeueSupplementary<T:UICollectionReusableView>(_ identifier: String? = nil, indexPath: IndexPath, kind: String) -> T {
let identifier = identifier ?? String(describing: T.self)
let view = self.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier, for: indexPath) as? T
switch view {
case .some(let unwrapped): return unwrapped
default: fatalError("Unable to dequeue" + T.self.description())
}
}
func register<T:UICollectionReusableView>(_ type: T.Type, identifier: String? = nil, kind: String) {
let identifier = identifier ?? String(describing: T.self)
register(type, forSupplementaryViewOfKind: kind, withReuseIdentifier: identifier)
}
}
+1
这就是我需要的!谢谢兄弟! – Hungcao
相关问题
- 1. 组合框边框
- 2. 轮组合框的边框
- 3. IE7中组合框的白色边框
- 4. 组边框
- 5. 一组边界框中的点分类
- 6. 框架边框上的Java组件
- 7. 中的CollectionView
- 8. ContainerView中的CollectionView
- 9. 如何在marionette.js中拥有CollectionView的CollectionView?
- 10. MPChart中的饼图边框边框
- 11. Collectionview Controller vs Collectionview
- 12. 如何在Swing中的组件边框外添加边距?
- 13. 分组多个边框
- 14. 在不同视图中显示CollectionView组?
- 15. WPF组合框/列表框或多个CollectionView的多个排序条件
- 16. 更改组合框边框颜色
- 17. Flex组合框 - 删除边框
- 18. 与IOS上的CollectionView弹出对话框
- 19. 设置CollectionView的框架尺寸
- 20. java中的边框
- 21. UIButton中的边框
- 22. 改变边界在分组框组件
- 23. 在SL组合框上使用CollectionView刷新时SelectedItemIndex不匹配
- 24. 如何更改组框中边框的颜色?
- 25. 在wpf c中更改组合框的边框#
- 26. 一些全息组件中的双边框/框架
- 27. 悬停框的动画边框(边框左侧,边框顶部)
- 28. PopView在CollectionView中
- 29. Swing Draw GridBagLayout组件中组件之间的1px边框线
- 30. iOS - 水平CollectionView中的UITableView,VerticalScrollView中的水平collectionView
拿一个UITableView和你的tableview的每个索引你可以有一个的CollectionView。你可以给每个tableview单元的角半径 – Developer
是啊一切都会更容易,如果我可以使用UITableView :(,问题是我必须使用UICollectionView :( – Hungcao
然后使用两个collectionviews – Developer