问题描述
我正在使用收藏夹视图, 我需要将单元格不位于顶部,而是位于中心上方,并添加标题,以便标题也可滚动,但是我不知道如何执行此操作,我将保留屏幕截图我想做
viewController
import UIKit
class ViewController: UIViewController {
var collectionView: UICollectionView!
var dataSource: UICollectionViewDiffableDataSource<Section,Video>?
var dataray = Section.allSections
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .orange
setupCollectionView()
createDataSource()
reloadData()
}
func setupCollectionView() {
collectionView = UICollectionView(frame: view.bounds,collectionViewLayout: createLayout())
collectionView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
collectionView.backgroundColor = .white
collectionView.showsHorizontalScrollIndicator = true
collectionView.showsverticalScrollIndicator = false
//collectionView.translatesAutoresizingMaskIntoConstraints = false
//collectionView.automaticallyAdjustsScrollIndicatorInsets = false
view.addSubview(collectionView)
collectionView.register(CollectionViewCell.self,forCellWithReuseIdentifier: CollectionViewCell.reuseIdentifier)
collectionView.register(SectionHeaderReusableView.self,forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,withReuseIdentifier: SectionHeaderReusableView.reuseIdentifier1)
// NSLayoutConstraint.activate([
// collectionView.topAnchor.constraint(equalTo: view.topAnchor,constant: 10),// collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor,// collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor,// collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor,constant: 10)
// ])
}
private func createLayout() -> UICollectionViewCompositionalLayout {
let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(170),heightDimension: .fractionalHeight(1))
let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)
layoutItem.contentInsets = NSDirectionalEdgeInsets.init(top: 0,leading: 16,bottom: 0,trailing: 0)
let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .absolute(170),heightDimension: .absolute(100))
let layoutGroup = NSCollectionLayoutGroup.horizontal(layoutSize: layoutGroupSize,subitems: [layoutItem])
//layoutGroup.contentInsets.bottom = 200
//layoutGroup.contentInsets = NSDirectionalEdgeInsets(top: 0,leading: 0,trailing: 16)
//layoutGroup.interItemSpacing = .fixed(5)
let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
layoutSection.orthogonalScrollingBehavior = .continuous
//layoutSection.accessibilityFrame = CGRect(x: 100,y: -100,width: 100,height: 100)
layoutSection.contentInsets = NSDirectionalEdgeInsets(top: 0,bottom: 16,trailing: 16)
//let layout = UICollectionViewCompositionalLayout(section: layoutSection)
let headerfooterSize = NSCollectionLayoutSize(
widthDimension: .absolute(150),heightDimension: .absolute(50)
)
// let headerfooterSize = NSCollectionLayoutSize(
// widthDimension: .fractionalWidth(1.0),// heightDimension: .estimated(30)
// )
//
let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: headerfooterSize,elementKind: UICollectionView.elementKindSectionHeader,alignment: .topLeading
)
// let headerfooterSize1 = NSCollectionLayoutSize(
// widthDimension: .absolute(200),// heightDimension: .estimated(30)
// )
//
// let sectionHeader1 = NSCollectionLayoutBoundarySupplementaryItem(
// layoutSize: headerfooterSize1,// elementKind: UICollectionView.elementKindSectionHeader,// alignment: .topLeading
// )
// sectionHeader.accessibilityScroll(.left)
//
layoutSection.boundarySupplementaryItems = [sectionHeader]
//layoutSection.boundarySupplementaryItems = [sectionHeader1]
let layout = UICollectionViewCompositionalLayout(section: layoutSection)
let config = UICollectionViewCompositionalLayoutConfiguration()
config.interSectionSpacing = 10
layout.configuration = config
return layout
}
func createDataSource() {
dataSource = UICollectionViewDiffableDataSource<Section,Video>(collectionView: collectionView) { collectionView,indexPath,app in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.reuseIdentifier,for: indexPath) as! CollectionViewCell
// cell.nameCategory.text = app.labelCell
// //cell.imageCategory.image = UIImage(named: app.imageCell!)
return cell
}
dataSource?.supplementaryViewProvider = { [weak self] collectionView,kind,indexPath in
guard let sectionHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind,withReuseIdentifier: SectionHeaderReusableView.reuseIdentifier1,for: indexPath) as? SectionHeaderReusableView else {
return nil
}
guard let firstApp = self?.dataSource?.itemIdentifier(for: indexPath) else { return nil }
guard let section = self?.dataSource?.snapshot().sectionIdentifier(containingItem: firstApp) else { return nil }
//if ((section.name?.isEmpty) != nil) { return nil }
sectionHeader.titleLabel.text = section.title//section.name
/
return sectionHeader
}
}
func reloadData() {
var snapshot = NSDiffableDataSourceSnapshot<Section,Video>()
snapshot.appendSections(dataray)
for section in dataray {
snapshot.appendItems(section.videos,toSection: section)
}
dataSource?.apply(snapshot)
}
}
CollectionViewCell
import UIKit
class CollectionViewCell: UICollectionViewCell {
static let reuseIdentifier: String = "FeaturedCell"
let nameCategory = UILabel()
let imageCategory = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
setupCell()
}
func setupCell() {
nameCategory.frame = CGRect(x: 1,y: 10,height: 20)
backgroundColor = .yellow
addSubview(nameCategory)
addSubview(imageCategory)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
SectionHeader
import UIKit
class SectionHeaderReusableView: UICollectionReusableView {
static let reuseIdentifier1 = "SectionHeader"
//let label = UILabel()
lazy var titleLabel: UILabel = {
var label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .title1).pointSize,weight: .bold)
label.adjustsFontForContentSizeCategory = true
label.textColor = .black
label.textAlignment = .left
label.numberOfLines = 1
label.setContentCompressionResistancePriority(.defaultHigh,for: .horizontal)
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
func setup() {
backgroundColor = .clear
addSubview(titleLabel)
NSLayoutConstraint.activate([
titleLabel.topAnchor.constraint(equalTo: topAnchor),titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor,constant: 16),titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor,constant: 0),titleLabel.bottomAnchor.constraint(equalTo: bottomAnchor,constant: -16)
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
型号
部分
import UIKit
// 1
class Section: Hashable {
var id = UUID()
// 2
var title: String
// 3
var videos: [Video]
init(title: String,videos: [Video]) {
self.title = title
self.videos = videos
}
// 4
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: Section,rhs: Section) -> Bool {
lhs.id == rhs.id
}
}
extension Section {
static var allSections: [Section] = [
Section(title: "SwiftUI",videos: [
Video(
title: "SwiftUI",thumbnail: UIImage(),lessonCount: 37,link: URL(string: "https://www.raywenderlich.com/4001741-swiftui")
),Video(
title: "SwiftUI1",Video(
title: "SwiftUI2",Video(
title: "SwiftUI3",link: URL(string: "https://www.raywenderlich.com/4001741-swiftui")
)//,// Video(
// title: "SwiftUI4",// thumbnail: UIImage(),// lessonCount: 37,// link: URL(string: "https://www.raywenderlich.com/4001741-swiftui")
// )
]),Section(title: "UIKit",videos: [
Video(
title: "Demystifying Views in iOS",lessonCount: 26,link:
URL(string:
"https://www.raywenderlich.com/4518-demystifying-views-in-ios")
),Video(
title: "Reproducing Popular iOS Controls",lessonCount: 31,link: URL(string: """
https://www.raywenderlich.com/5298-reproducing
-popular-ios-controls
""")
)
]),Section(title: "Frameworks",videos: [
Video(
title: "Fastlane for iOS",lessonCount: 44,link: URL(string:
"https://www.raywenderlich.com/1259223-fastlane-for-ios")
),Video(
title: "Beginning RxSwift",lessonCount: 39,link: URL(string:
"https://www.raywenderlich.com/4743-beginning-rxswift")
)
]),Section(title: "Miscellaneous",videos: [
Video(
title: "Data Structures & Algorithms in Swift",lessonCount: 29,link: URL(string: """
https://www.raywenderlich.com/977854-data-structures
-algorithms-in-swift
""")
),Video(
title: "Beginning ARKit",lessonCount: 46,link: URL(string:
"https://www.raywenderlich.com/737368-beginning-arkit")
),Video(
title: "Machine Learning in iOS",lessonCount: 15,link: URL(string: """
https://www.raywenderlich.com/1320561-machine-learning-in-ios
""")
),Video(
title: "Push Notifications",lessonCount: 33,link: URL(string:
"https://www.raywenderlich.com/1258151-push-notifications")
),]),Video(
title: "Push Notifications",link: URL(string:
"https://www.raywenderlich.com/1258151-push-notifications")
),link: URL(string:
"https://www.raywenderlich.com/1258151-push-notifications")
)
])
]
}
视频
import UIKit
class Video: Hashable {
var id = UUID()
var title: String
var thumbnail: UIImage?
var lessonCount: Int
var link: URL?
init(title: String,thumbnail: UIImage? = nil,lessonCount: Int,link: URL?) {
self.title = title
self.thumbnail = thumbnail
self.lessonCount = lessonCount
self.link = link
}
// 1
func hash(into hasher: inout Hasher) {
// 2
hasher.combine(id)
}
// 3
static func == (lhs: Video,rhs: Video) -> Bool {
lhs.id == rhs.id
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)