CollectionView标题在自动旋转时不会调整大小

时间:2015-06-09 06:48:17

标签: ios swift uicollectionview uicollectionreusableview

我在CollectionView中有一个headerView,并根据Label的文本大小以编程方式调整了headerView的大小。在旋转到横向方向时,可重复使用的标题视图不会自动调整大小,但在滚动时,它会自行调整大小以提供预期的结果。 In portrait mode In landscape mode before scrolling

In Landscape mode after scrolling

以下是我用来调整标题大小的代码段。

    echo Checking for Administrator elevation...
openfiles > NUL 2>&1
if %errorlevel%==0 (
    echo Elevation found! Proceeding...
    goto menu
) else (
    echo You are not running as Administrator...
    echo This batch cannot do it's job without elevation!
    echo.
    echo Right-click and select ^'Run as Administrator^' and try again...
    echo.
    echo Press any key to exit...
    pause > NUL
    exit
)

CollectionViewHeader是一个继承UIcollectionReusableView的自定义类,包含headerText作为UILabel。当方向改变时,有没有办法阻止可重用视图恢复到原始大小?

2 个答案:

答案 0 :(得分:3)

对我来说,我有一个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)
}

答案 1 :(得分:0)

您必须更新方向的UICollectionView流程布局。

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {

    collectionView.performBatchUpdates({ () -> Void in

    }, completion: { (complete) -> Void in

    })

}
相关问题