Ios 如何在collectionView顶部使用UIView,以便在滚动单元格时,UIView不应移动?
我正在使用collectionView单元格创建水平旋转木马,我想应用一个在collectionView顶部有标签的自定义视图,这样当我滚动单元格时,UIView应该保持不变,但更改标签和单元格的文本应该会更改。请帮助我。您可以在Ios 如何在collectionView顶部使用UIView,以便在滚动单元格时,UIView不应移动?,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在使用collectionView单元格创建水平旋转木马,我想应用一个在collectionView顶部有标签的自定义视图,这样当我滚动单元格时,UIView应该保持不变,但更改标签和单元格的文本应该会更改。请帮助我。您可以在collectionView节头中加载UIView(),如下所示 使用类类型创建标题XibUICollectionReusableView 注册您刚刚创建的Xib collectionFeatured.register(TodaySectionHeader.nib,
collectionView
节头中加载UIView()
,如下所示
使用类类型创建标题XibUICollectionReusableView
注册您刚刚创建的Xib
collectionFeatured.register(TodaySectionHeader.nib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "TodaySectionHeader")
CollectionView
Delegate
和DataSource
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 80)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let sectionHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "TodaySectionHeader", for: indexPath) as! TodaySectionHeader
sectionHeader.labelDate.text = Date().toString(format: "EEEE dd MMMM").uppercased()
sectionHeader.labelTitle.text = "Today"
return sectionHeader
}
如果您现在想将该标题粘贴在顶部,则必须在采集初始化期间设置UICollectionViewFlowLayout
,其中设置委托数据源
,并基本上在viewDidLoad
中注册xib
let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout // casting is required because UICollectionViewLayout doesn't offer header pin. Its feature of UICollectionViewFlowLayout
layout?.sectionHeadersPinToVisibleBounds = true
希望这是你所期待的
出于演示目的,以编程方式完成了所有操作。这就是CollectionViewController
和整个代码的样子
//
// SliderController.swift
// AssignmentSO
//
// Created by Chanaka Caldera on 7/5/19.
// Copyright © 2019 StackOverflow. All rights reserved.
//
import UIKit
private let reuseIdentifier = "Cell"
class SliderController: UICollectionViewController {
var topview: UIView!
var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//MARK: - set up top view and label
topview = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
topview.backgroundColor = .green
view.addSubview(topview)
label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
label.text = "Title 0.0"
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 40)
topview.addSubview(label)
// MARK: - Registering the cell and set EdgeInsets (if you are using storyboard set EdgeInset would be enough
self.collectionView!.register(CustomCell.self, forCellWithReuseIdentifier: reuseIdentifier)
self.collectionView.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)
self.collectionView.isPagingEnabled = true
}
}
// MARK: Datasource
extension SliderController {
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomCell
cell.label.text = "Cell \(indexPath.row)"
return cell
}
}
extension SliderController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = UIScreen.main.bounds.height - 100
let width = UIScreen.main.bounds.width
return CGSize(width: width, height: height)
}
}
extension SliderController {
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let xvalue = scrollView.contentOffset.x
let width = UIScreen.main.bounds.width
let cellIndex = xvalue/width
switch cellIndex {
case 0:
label.text = "Title \(cellIndex)"
case 1:
label.text = "Title \(cellIndex)"
case 2:
label.text = "Title \(cellIndex)"
case 3:
label.text = "Title \(cellIndex)"
case 4:
label.text = "Title \(cellIndex)"
default:
label.text = ""
}
}
}
//MARK: Custom cell
class CustomCell: UICollectionViewCell {
var label: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .lightGray
label = UILabel(frame: CGRect(x: 0, y: 0, width: contentView.bounds.size.width, height: contentView.bounds.size.height))
label.font = UIFont.systemFont(ofSize: 40)
label.text = "2"
label.textAlignment = .center
addSubview(label)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
希望这将有助于某人,并在相同的文件中做了演示目的的每件事。对不起。干杯 将视图作为同级添加到集合视图?如何作为同级添加,因为rootcontroller是UICollectionViewController?不要使用UICollectionViewController。使用UIViewController。您可以将根视图控制器更改为经典的UIViewController
,它同时嵌入转盘和集合视图。您可以使用子控制器保留集合视图控制器。感谢您的关注。我用所有UICollectionViewController启动了这个项目。我需要更改整个代码。如果有其他的方法,请帮助我,请分享更多的细节和问题代码,所以我会尽力帮助你是的,非常感谢。我可以通过电子邮件发送吗。因为这是一堆代码,不可能在这里共享?是的,兄弟。谢谢你的关注。