3

私が作成したテスト プロジェクトに、Interface Builder を使用しない UICollectionView があります。アプリを実行すると、データソースを介してコレクション ビューに与えたテスト ビューが右上隅の (0,0) 付近に表示されます。そして、私は一生、理由を理解できません。セルのコンテンツ ビューに制約を追加しようとしました。item insets delegate 関数をいじってみましたが、違いはないようです。何か不足していますか?

テストビューコントローラーのコードは次のとおりです。

#import "TestViewViewController.h"

@interface TestViewViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout *flowLayout;
@property (strong, nonatomic) NSMutableArray *testViews;

@end

@implementation TestViewViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView {


    self.view = [[UIView alloc] init];
    self.view.backgroundColor = [UIColor whiteColor];

    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];

    self.testViews = [[NSMutableArray alloc] init];

    UIView *testView = [[UIView alloc] init];
    testView.backgroundColor = [UIColor blueColor];
    testView.translatesAutoresizingMaskIntoConstraints = NO;

    UILabel *testLabel = [[UILabel alloc] init];
    testLabel.translatesAutoresizingMaskIntoConstraints = NO;
    testLabel.text = @"I hate collection views.";

    [testView addSubview:testLabel];

    testView = [[UIView alloc] init];
    testView.backgroundColor = [UIColor redColor];
    testView.translatesAutoresizingMaskIntoConstraints = NO;

    testLabel = [[UILabel alloc] init];
    testLabel.translatesAutoresizingMaskIntoConstraints = NO;
    testLabel.text = @"I really do.";

    [testView addSubview:testLabel];


    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];


    NSDictionary *views = @{@"collectionView": self.collectionView};

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[collectionView]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[collectionView]|" options:0 metrics:nil views:views]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
    return self.testViews.count;
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    NSLog(@"%i", self.testViews.count);
    [cell.contentView addSubview: self.testViews[indexPath.row]];

    return cell;
}

#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

//    UIView *statView = self.testViews[indexPath.row];
    return CGSizeMake(100.0, 100.0);
}


@end
4

1 に答える 1