【问题标题】:UICollectionView Section headers & footers show wrong way - iOSUICollectionView 部分页眉和页脚显示错误的方式 - iOS
【发布时间】:2016-07-28 07:42:01
【问题描述】:

问题是: SupplementaryView 上面有一个textLabel,但是文本显示在错误的位置,而页眉和页脚以及 textLabel 显示正确。

正如您在图片中看到的那样,只有第一节标题有标题,其他页眉或页脚文本是看不见的(它们在屏幕上,但您必须向下滚动才能看到它)。
不知道怎么解决...

我在viewDidLoad 函数中设置了我的collectionview,如下所示:

func setupCollectionsView(){
    let width:CGFloat = self.view.frame.size.width/4
    let flowLayout = UICollectionViewFlowLayout()
    flowLayout.minimumInteritemSpacing = 5
    flowLayout.minimumLineSpacing = 5
    flowLayout.estimatedItemSize = CGSizeMake(width, width)
    flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 30)
    flowLayout.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 30)
    flowLayout.scrollDirection = UICollectionViewScrollDirection.Vertical
    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: flowLayout)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.registerClass(FuncViewCell.self, forCellWithReuseIdentifier: "collectionView")
    collectionView.backgroundColor = UIColor.clearColor()
    collectionView.registerClass(SupplementaryView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerView")
    collectionView.registerClass(SupplementaryView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footerView")
    self.view.addSubview(collectionView)
}

对于每个补充视图,我都写了这个:

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    var view:SupplementaryView?
    var title:String = ""
    switch kind {
    case UICollectionElementKindSectionFooter:
        view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "footerView", forIndexPath: indexPath) as? SupplementaryView
        view?.backgroundColor = UIColor ( red: 0.9879, green: 0.3225, blue: 0.4925, alpha: 1.0 )
        title = "Footer"
    default:
        view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "headerView", forIndexPath: indexPath) as? SupplementaryView
        view?.backgroundColor = UIColor ( red: 0.6571, green: 1.0, blue: 0.8628, alpha: 1.0 )
        title = "Header"
    }
    switch indexPath.section {
    case 0:
        title = "First \(title)"
    default:
        title = "Second \(title)"
    }
    view?.setTitle(title)
    return view!
}

我的补充观点是这样实现的:

class SupplementaryView: UICollectionReusableView{
var titleLabel:UILabel!
override init(frame: CGRect) {
    super.init(frame: frame)
    titleLabel = UILabel(frame: frame)
    self.addSubview(titleLabel)
    let gesture = UITapGestureRecognizer(target: self, action: #selector(SupplementaryView.tap))
    self.addGestureRecognizer(gesture)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setTitle(title:String){
    print("Label's frame is\(titleLabel.frame)")
    titleLabel.text = title
}

func tap(){
    print("\(titleLabel.text!) tapped")
}}

PS:我打印每个文本标签的框架,它们正好在正确的位置。 .

【问题讨论】:

    标签: swift uicollectionview sectionheader


    【解决方案1】:

    改变你的代码如下,看看它是否有效

    override init(frame: CGRect) {
         super.init(frame: frame)
        // There should be self.bounds
         titleLabel = UILabel(frame: self.bounds)
         self.addSubview(titleLabel)
         let gesture = UITapGestureRecognizer(target: self, action: #selector(SupplementaryView.tap))
         self.addGestureRecognizer(gesture)
    }
    

    【讨论】:

    • 我刚试过,效果很好!你拯救了我的一天!我已经花了一天多的时间!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2015-03-04
    • 2017-11-30
    • 2017-04-03
    • 1970-01-01
    相关资源
    最近更新 更多