1

我想要做的是对齐阵列中的网格中的视图。我已经创建了视图和数组。

for (int i = 0; i < [directoryContents count]; i++){


    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,120)];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 100, 100);
    button.accessibilityIdentifier = [directoryContents objectAtIndex:i];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 100, 20)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [directoryContents objectAtIndex:i];



    [containerView addSubview:button];
    [containerView addSubview:label];

    [self.view addSubview:containerView];

    [_containerViewArray addObject:containerView];



}
[self layoutViews:_containerViewArray inView:self.view];

我所做的工作是将它们彼此对齐,我遇到的问题是我需要让它们包裹起来而不是离开边缘。我正在为他们做的事情是这样的

    - (void)layoutViews:(NSArray *)views inView:(UIView *)contentView
{

    NSInteger overallWidth = 0;
    for ( UIView *view in views ) {
        overallWidth += view.frame.size.width;
    }

    CGSize contentSize = contentView.frame.size;

    CGFloat startOffset = (contentSize.height - overallWidth)/2.0;
    CGFloat offset = startOffset;
    for ( UIView *view in views ) {
        CGRect frame = view.frame;
        frame.origin.x = offset;
        view.frame = frame;
        offset += view.frame.size.width;
    }
    }
4

2 回答 2

1

这是我在 UICollectionView 发布之前使用的,易于修改以适应网格中所需的行数以及视图的大小。

NSUInteger buttonWidth = 100;
NSUInteger buttonHeight = 100;
NSUInteger space = 5;


float scrollContentY = (ceilf((float)imageNames.count / 3.0f) * (buttonHeight + space));
[myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    int row = idx / 3;
    int column = idx % 3;

    UIView *view = [UIView new];
    [view setBackgroundColor:[UIColor redColor]];
    view.frame = CGRectMake((buttonWidth + space) * column + space, (buttonHeight + space) * row + space , buttonWidth, buttonHeight);
    [view setTag:(NSInteger)idx];

    [self.scrollView addSubview:view];
    [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width, scrollContentY)];
}];

请注意,如果您有一个大数组要枚举,这可能是不可取的,因为它没有任何类型的可重用视图。它将为数组中的每个项目分配/初始化一个视图。

于 2012-12-21T18:18:10.960 回答
1

如果我们假设我们有一个部分数组(您的视图)并且您需要每行布局 3 个项目,则视图的大小为 98*98,水平间距为 6.5px,垂直间距为 8px

for (int i = 0; i< [sectionsArray count]; i++) {
    int row = (int)i/3;
    int col = i%3;
    float x = 6.5 + (104.5*col);
    float y = 8 + (106*row);
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(x, y, 98, 98)];
    sectionView.backgroundColor = [UIColor clearColor];
    //button
    UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [sectionButton setTintColor:[UIColor grayColor]];
    sectionButton.frame = CGRectMake(0, 0, 98, 98);
    sectionButton.tag = i+100;
    [sectionButton addTarget:self action:@selector(showSectionDetail:) forControlEvents:UIControlEventTouchUpInside];
    [sectionView addSubview:sectionButton];

    //adding title
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 75, 98, 20)];
    titleLabel.textAlignment = UITextAlignmentCenter;
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.font=[UIFont fontWithName:@"Helvetica" size:14];
    titleLabel.textColor = [UIColor colorWithRed:241.0/255.0 green:124.0/255.0 blue:22.0/255.0 alpha:1.0];
    titleLabel.text = [[sectionsArray objectAtIndex:i] objectForKey:@"Title"];
    [sectionView addSubview:titleLabel];
    [titleLabel release];
    [scroll addSubview:sectionView];        
    [sectionView release];
}
int numberOfRows;
if ([sectionsArray count]/3.0f == 0) {
    numberOfRows = [sectionsArray count]/3;
}else{
    numberOfRows = [sectionsArray count]/3 +1;

}
scroll setContentSize:CGSizeMake(320, numberOfRows*106);
于 2012-12-21T19:07:14.837 回答