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