[英]CollectionView header doesn't resize on auto rotate
我的CollectionView中有一個headerView,並已根據Label的文本大小以編程方式調整了headerView的大小。 旋轉到橫向時,可重復使用的標題視圖不會自動調整大小,但滾動時會自動調整大小以提供預期的結果。
以下是我用來調整標頭大小的代碼段。
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind{
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "HeaderView", forIndexPath: indexPath) as! CollectionViewHeader
var headerString = westBeaches[indexPath.section] as NSString
var newSize: CGSize = headerString.sizeWithAttributes([NSFontAttributeName:headerView.headerText.font])
headerView.frame.size.width = newSize.width + 20
headerView.layer.cornerRadius = 15
headerView.headerText.text = westBeaches[indexPath.section]
headerView.center.x = collectionView.center.x
headerView.alpha = 0.7
return headerView
default:
assert(false, "Unexpected element Kind")
}
}
CollectionViewHeader是繼承自UIcollectionReusableView的自定義類,並且包含headerText作為UILabel。 當方向改變時,有什么方法可以防止可重用的視圖恢復其原始大小嗎?
對我來說,我有一個UISearchBar作為標題,並且有同樣的問題。 也就是說,旋轉后搜索欄沒有填滿CollectionView寬度。
我通過在旋轉動畫旁邊設置搜索欄的寬度來固定它。
在UIViewController
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
// Animate searchBar too
coordinator.animateAlongsideTransition({ [unowned self] _ in
var frame = self.searchController.searchBar.frame
frame.size.width = size.width
self.searchController.searchBar.frame = frame
}, completion: nil)
}
您必須更新UICollectionView流布局以獲取方向。
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
collectionView.performBatchUpdates({ () -> Void in
}, completion: { (complete) -> Void in
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.