iOS 6.0:UICollectionView不尊重使用pagingEnabled的clipsToBounds [英] iOS 6.0: UICollectionView doesn't respect clipsToBounds with pagingEnabled

查看:575
本文介绍了iOS 6.0:UICollectionView不尊重使用pagingEnabled的clipsToBounds的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我正在实施 UICollectionView (这是第一次)努力实现瓷砖的分页水平滚动视图。我希望每个瓷砖显示在框架的中央,它的左边和右边部分可见的姐妹瓷砖(类似于Safari应用程序中的页面选择器)。我有兴趣使用 UICollectionView 来利用内置的单元格出列,而不是使用旋转的 UITableView

I am implementing a UICollectionView (for the first time) in an effort to achieve a paged horizontal scroll view of tiles. I'd like each tile to show in the center of the frame with it's sister tiles partially visible to the left and right (something like the page selector in the Safari app). I'm interested in using the UICollectionView to take advantage of built-in cell dequeueing and would rather not use a rotated UITableView.

问题

我发现的问题是在使用时 pagingEnabled = YES clipsToBounds = NO UICollectionView 删除分页完成后, collectionView 框架外的单元格(它们不在 visibleCells 数组中)。任何人都可以提供如何在保持这种基本设置的同时实现显示姐妹瓷砖预览效果的建议吗?或者我接近这个错误?

The issue I'm finding is that when using pagingEnabled = YES and clipsToBounds = NO, the UICollectionView removes cells outside the collectionView frame (they're not in the visibleCells array) as soon as paging is complete. Can anyone provide advice on how to achieve the effect of displaying previews of the sister tiles while maintaining this basic setup? Or am I approaching this incorrectly?

屏幕截图

start

滚动

结束

start scrolling end

滚动屏幕完全正确。但是在开始和结束镜头中,我希望在蓝色边缘可以看到绿色。

The scrolling screen is exactly correct. But in the start and end shots I want there to be green visible in the blue margins.

这是我的AppDelegate.m中的内容(归功于 tutsplus.com 这里的基本设置):

Here's what's in my AppDelegate.m (credit to tutsplus.com for the basic setup here):

#import "AppDelegate.h"

@interface ViewController : UICollectionViewController
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ID"];
    [self.view setBackgroundColor:[UIColor blueColor]];
    // pad the collection view by 20 px
    UIEdgeInsets padding = UIEdgeInsetsMake(20.0, 20.0, 20.0, 20.0);
    [self.collectionView setFrame:UIEdgeInsetsInsetRect(self.view.frame, padding)];
    // set pagingEnabled and clipsToBounds off
    [self.collectionView setPagingEnabled:YES];
    [self.collectionView setClipsToBounds:NO];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath];
    UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"%d", indexPath.row];
    [label setBackgroundColor:[UIColor greenColor]];
    [cell.contentView addSubview:label];
    return cell;
}
@end

@implementation AppDelegate
{
    ViewController *vc;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // setup the UICollectionViewFlowLayout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(280, 280);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // add a custom UICollectionViewController to the window
    vc = [[ViewController alloc] initWithCollectionViewLayout:layout];
    self.window.rootViewController = vc;
    self.window.backgroundColor = [UIColor yellowColor];
    [self.window makeKeyAndVisible];
    return YES;
}
@end


推荐答案

原来解决方案实际上非常简单。我只需要用足够的像素重叠 UICollectionViewCell 单元格,让它们在页面滚动完成后仍然显示在collectionView的框架内。相关代码是

Turns out the solution to this was actually quite simple. I just needed to overlap the UICollectionViewCell cells by enough pixels to have them still show within the collectionView's frame after the paged scrolling finishes. The relevent code was

layout.itemSize = CGSizeMake(300, 300);
layout.minimumLineSpacing = -20.0;

我将 UICollectionViewFlowLayout 子类化并覆盖了(CGSize)collectionViewContentSize 返回单元格的非重叠大小的方法。

And I subclassed the UICollectionViewFlowLayout and overrode the (CGSize)collectionViewContentSize method to return the non-overlapped size of the cells.

这篇关于iOS 6.0:UICollectionView不尊重使用pagingEnabled的clipsToBounds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆