开发者

Adding subviews in UIScrollView in a grid pattern

开发者 https://www.devze.com 2023-02-04 18:37 出处:网络
I am adding subviews to a UIScrollView, (which are a UIVewController), and they are not forming a 2 x 2 grid like they are suppose to. Here is some of the code I am using:

I am adding subviews to a UIScrollView, (which are a UIVewController), and they are not forming a 2 x 2 grid like they are suppose to. Here is some of the code I am using:

-(void)layoutSubviews{

    BOOL blurbsHaveBorder = YES;

    int blurbCount = 3;
    int blurbsPerRow;

    int viewWidth = gridView.bounds.size.width;
    int viewHeight = gridView.bounds.size.height;

    int blurbWidth = 320;
    int blurbHeight = 320;

    int spaceWidth = round((viewWidth -blurbWidth * blurbsPerRow) / (blurbsPerRow + 1));
    int spaceHeight = spaceWidth;

    int rowCount = ceil(blurbCount / (float)blurbsPerRow);
    int rowHeight = blurbHeight + spaceHeight;


    if (blurbsPerRow < 1) blurbsPerRow = 1;


    NSInteger rowsPerView = viewHeight / rowHeight;
    NSInteger topRow = gridView.contentOffset.y / rowHeight;
    NSInteger bottomRow = topRow + rowsPerView;

    NSInteger startAtIndex = topRow * blurbsPerRow;
    NSInteger stopAtIndex = (bottomRow * blurbsPerRow) + blurbsPerRow;
    if (stopAtIndex > blurbCount) stopAtIndex = blurbCount;

    int x = spaceWidth;
    int y = spaceHeight + (topRow * rowHeight);

    int i;
    /*  add new subviews.  */ 
    for (int i = startAtIndex; i < stopAtIndex; i++) {
        if (i >= 0) {

            blurbItem = [[BlurbItem alloc] initWithFrame:CGRectMake(5,5,blurbWidth,blurbHeight) 
                                            andHasBorder:blurbs开发者_如何学GoHaveBorder];

            [gridView addSubview:blurbItem.view];
            [blurbItem release];
        }   

    }

    /*  adjust the position.  */
    if ( (i+1) % blurbsPerRow == 0) {
        /*  start new row.  */
        x = spaceWidth;
        y += blurbHeight + spaceHeight;
    } else {
        x += blurbWidth + spaceWidth;
    }
}


Am I reading this right? It looks like you're not setting blurbsPerRow. Would that make it default to 0?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号