パトリックはヘッダーの高さを変更するための正しい答えを出しましたが、質問で言ったように、ヘッダー機能が別のクラスにあったため、実際のviewControllerクラスにアクセスする方法がよくわかりませんでした。
シンプルにしておくと、ストーリーボードからそのクラスへのアクション リンクを作成するだけで済みます。ボタン クリックの応答だけでなく、さらにいくつかの操作を行う必要がありました。これは私がしたことです:
class MyViewController : UIViewController, UICollectionDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
var headerHeight: CGFloat = 100.0
var shouldChangeHeader : Bool? = false
let loginHeaderSectionIndex: Int = 0
func setHeaderHeight(height : CGFloat!)
{
headerHeight = height
shouldChangeHeader = true
self.collectionView.collectionViewLayout.invalidateLayout()
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind
{
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "loginHeader", forIndexPath: indexPath) as! LoginHeaderView
//you should already have this method, LoginHeaderView is the class with all the functionality for the header, we register a callback.
headerView.registerHeightChangeCallback(setHeaderHeight)
return headerView
}
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
//here we actually change the height, best is to include a if-condition, so it won't change on loading unless you especially call it.
if self.shouldChangeHeader && self.loginHeaderSectionIndex == section {
shouldChangeHeader = false
return CGSize(width: collectionView.bounds.width, height: headerHeight )
}
else {
//do nothing, keep as it is now.
return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height) /
}
}
}
class LoginHeaderView : UICollectionReusableView {
var heightCallback : ((height : CGFloat!)-> Void)?
func registerHeightChangeCallback(callback :((height : CGFloat!)-> Void)?)
{
heightCallback = callback
}
@IBAction func buttonClicked( sender: AnyObject? ) { //or from any other method...
if(heightCallback != nil)
{
heightCallback!(height: 200)
}
}
}