2016-07-28 7 views
0

Проблема в следующем: SupplementaryView имеет на нем textLabel, но текст отображается в неправильном месте, а верхний и нижний колонтитулы и textLabel просто отображаются справа.UICollectionView Раздел заголовков и нижних колонтитулов показывает неправильный путь - iOS

Как вы можете видеть на картинке, только заголовок первого раздела имеет заголовок, другой текст верхнего или нижнего колонтитула скрыт (они находятся на экране, но вам нужно прокрутить вниз, чтобы увидеть его).
я не знаю, как ее решить ...

настроить мой CollectionView как следующее viewDidLoad функции:

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) 
} 

Для каждого supplementaryview я это пишу:

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: Я печатаю рамку каждой текстовой метки, они находятся в правильном положении. enter image description here.

ответ

0

Изменения вы код, как показано ниже, и посмотреть, если он работает

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) 
} 
+0

Я просто попытался, и он работал отлично! Вы спасли мой день! Я потратил больше одного дня на это! – lwiu