簡體   English   中英

CollectionView方向問題

[英]CollectionView orientation issues

我目前有一個帶有圖像網格的集合視圖,當選擇一個圖像時,它會切換到另一個帶有全屏圖像的集合視圖。

為此,我正在使用:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"collectionView"])

    {
        QuartzDetailViewController *destViewController = (QuartzDetailViewController *)segue.destinationViewController;

        NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];

        destViewController.startingIndexPath = indexPath;

        [destViewController.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];

        [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
    }
}

然后在我的詳細視圖中:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (_startingIndexPath) {
    NSInteger currentIndex = floor((scrollView.contentOffset.x - scrollView.bounds.size.width / 2) / scrollView.bounds.size.width) + 1;
    if (currentIndex < [self.quartzImages count]) {
        self.title = self.quartzImages[currentIndex][@"name"];
    }
}
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    layout.itemSize = self.view.bounds.size;

    [self.collectionView scrollToItemAtIndexPath:self.startingIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}

當我旋轉以橫向顯示圖像時,圖像消失並且頂部的標題發生更改,如果我返回到網格然后再次選擇一個單元格,它將再次選擇到詳細視圖,但是標題是針對另一個單元格,並且圖像顯示一半和兩個不同圖像之間的一半。

旋轉設備時,我也在日志中收到以下消息:

2013-06-04 17:39:53.869 PhotoApp[6866:907] the behavior of the UICollectionViewFlowLayout is not defined because:
2013-06-04 17:39:53.877 PhotoApp[6866:907] the item height must be less that the height of the UICollectionView minus the section insets top and bottom values.

我該如何糾正它,以便它可以支持定向。

謝謝

由於縱向的像元大小對於橫向而言太大,因此您需要使集合視圖的布局無效。

在您的父UIViewController中編寫這樣的內容應該可以解決您的問題:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
  [self.collectionView.collectionViewLayout invalidateLayout];
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM