1

我有一个 uiview,我必须在 uiview 的中心并排放置几个按钮,如图所示以编程方式

和这个图类似。按钮应该放在uiview的中心

 --------------------------------
| ----     ----      ----        |
||    |   |    |    |    |       |
|| 1B |   | 3B |    | 5B |       |
| ----     ----      ----        |
| ----     ----                  |
|| 2B |   |    |                 |
||    |   | 4B |                 |
| ----     ----                  |
 --------------------------------
4

3 回答 3

1

此方法将为每行创建 4 个按钮,与 IOS 中的 PhotoGallery 相同 进行所需的更改以实现您的输出

-(void)createButtons
{
    int x, y, width, height, gapBetweenTwoButtons;
    x = y = 4;
    gapBetweenTwoButtons = 4;
    int numberOfButtons = 10;

    // i have calculated width for 4 columns it may vary for your requirement
    width = height = 140;

    // loop to create buttons
    UIButton *btn;

    for (int i=0; i< numberOfButtons; i++)
    {
        btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setFrame:CGRectMake(x, y, width/2, height/2)];

        // Add button to scrollview
        [scrlViewSubCategory addSubview:btn];

        // calculate x and y accordingly
        if((i+1) % 4 == 0)
        {
            x = gapBetweenTwoButtons;
            y += (width/2)+gapBetweenTwoButtons;
        }
        else
            x += (width/2)+gapBetweenTwoButtons;
    }

    // finally , adjust content size of scrollview

    [scrlViewSubCategory setContentSize:CGSizeMake(scrlViewSubCategory.frame.size.width, (numberOfButtons/4) * ((width/2)+4))];
}
于 2013-03-29T06:32:40.883 回答
1

这是我已经解决的问题。源代码

一个枚举类型,表示您想要垂直或水平排列的项目

typedef enum {
    ArrangementTypeHorz = 1,
    ArrangementTypeVert = 2
}ArrangementType;

声明的数量或行列和排列类型

@interface ATTViewController ()

{
    ArrangementType _arrangementType;

    NSUInteger _rows;
    NSUInteger _columns;

}

@end

方法计算项目索引wrt superView的框架

- (CGRect)frameForItemAtIndex:(NSUInteger)index inSuperView:(UIView *)superView
{
    CGFloat blockWidth  = floorf(superView.frame.size.width/_columns);
    CGFloat blockHeight = floorf(superView.frame.size.height/_rows);

    NSUInteger row    = 0;
    NSUInteger column = 0;

    if (_arrangementType == ArrangementTypeHorz) {
        row    = floorf((CGFloat)index/_columns);
        column = index - row*_columns;
    }else{
        column    = floorf((CGFloat)index/_rows);
        row = index - column*_rows;
    }

    CGFloat xOffset = 5.0f;
    CGFloat yOffset = 5.0f;

    return  CGRectMake(blockWidth*column+xOffset,
                       blockHeight*row+yOffset,
                       blockWidth-(2*xOffset),
                       blockHeight-(2*yOffset));
}

将按钮添加到 containerView。添加时设置行、列和排列类型

- (void)addButtonsToContainerView
{
    NSUInteger numberOfItems = 5;

    _rows = 2;
    _columns = 3;

    _arrangementType = ArrangementTypeVert;

    NSUInteger itemCount = 0;

    while (itemCount<numberOfItems)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        CGRect frame = [self frameForItemAtIndex:itemCount inSuperView:self.containerView];
        [button setFrame:frame];
        NSString *title = [NSString stringWithFormat:@"Button %d",itemCount+1];
        [button setTitle:title forState:UIControlStateNormal];

        [self.containerView addSubview:button];

        itemCount ++;
    }
}
于 2013-03-29T07:25:38.603 回答
0

试试这个:

    -(void) fillGridViewAfterCall 
    {
            int count = [viewGrid.subviews count];
            for (int i = count-1; i >= 0; i--) 
            {
                    [[viewGrid.subviews objectAtIndex:i] removeFromSuperview];
            }
            int videoCount = 0;
            int posx = 10;
            int posy = 10;
            int mainLoopCount = ([arrUserVideos count]/3);

            if (([arrUserVideos count]%3) > 0) 
            {
                    mainLoopCount = mainLoopCount + 1;
            }

            for (int i=0; i<mainLoopCount; i++) 
            {
                    posx = 10;
                    for (int j=0; j<3; j++) 
                    {
                            VideoShare *shareObj = [arrUserVideos objectAtIndex:videoCount];

                            UIButton *btnVideo = [[[UIButton alloc] init];
                            btnVideo.frame = CGRectMake(posx, posy, 96, 96);
                            btnVideo.tag = shareObj.vdId;
                            btnVideo.layer.masksToBounds = YES;
                            btnVideo.layer.cornerRadius = 4.0;
                            [btnVideo setImage:[UIImage imageNamed:@"iconGridSelected.png"] forState:UIControlStateNormal];
                            [btnVideo addTarget:self action:@selector(btnGridVideoClick:) forControlEvents:UIControlEventTouchUpInside];
                            [viewGrid addSubview:btnVideo];
                            videoCount++;

                            if (videoCount == [arrUserVideos count]) 
                            {
                                    break;
                            }
                            posx = posx + 102;
                    }
                    posy = posy + 102;
            }

            if (arrUserVideos.count == 0) 
            {
                    viewGrid.hidden = YES;
            }
            else 
            {
                    viewGrid.frame = CGRectMake(0, posyDynamic, 320, (posy+5));
                    viewGrid.hidden = NO;
            }

            scrollObj.contentSize = CGSizeMake(320, (viewGrid.frame.size.height+100+scrollIncrement));
    }
于 2013-03-29T06:33:50.500 回答