如何在ScrollView中滚动随机高度的UIView



我在界面构建器中创建了ScrollView和UIView(但也可以是代码)。我想添加随机数量的缩略图(子视图)到UIView,并能够滚动它向上向下,如果有更多的屏幕可以采取。然而,我无法让ScrollView滚动。

我在哪里以及如何调整UIView并将其添加为ScrollView的子视图?

何时设置scrollView的contentSize使其与uiview一起展开?我试过创建固定的大内容大小,但效果不佳。

我可能需要改变IB中的哪些属性才能使其工作?

我想在下一步中使uiview中的图像可点击。

我想我也可以在没有IB的代码中制作两个视图,只是不知何故不能让它工作。

//声明基视图

UIViewController *viewForLoadForm =[[UIViewController alloc] init];
viewForLoadForm.view.frame=CGRectMake(0, 0,screenSize.size.width,screenSize.size.height);
viewForLoadForm.view.backgroundColor=[UIColor blackColor];
[Manview.view addSubview:viewForLoadForm];

//声明scrollview并添加到基视图

UIScrollView *scrollview=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenSize.size.width,screenSize.size.height)];
scrollview.indicatorStyle=UIScrollViewIndicatorStyleBlack;
[scrollview setContentSize:CGSizeMake(screenSize.size.width,screenSize.size.height)];
scrollview.clipsToBounds = NO;
scrollview.scrollEnabled = YES;
scrollview.pagingEnabled = NO;              scrollview.showsVerticalScrollIndicator =NO;
scrollview.alwaysBounceVertical= YES;
[viewForLoadForm.view addSubview:scrollview];

//将控件添加到滚动视图,即图像视图或其他需要显示的内容

.........your control's code (textbox,imageview etc)

//获取最后一次控制的y位置并设置你的K

float k =最后一个控件的y轴位置+100;(这将决定滚动条的大小)

// reassign the scrollview height
    [scrollview setContentSize:CGSizeMake(screenSize.size.width,k)];

使用上面的行在运行时调整滚动视图的大小。

screenSize.size。这里的宽度是320

代码段,
- (void) createThumbView
{
    float y_axis = Set y axis;
    int x = 0;
    int totalImgs = total images;
    int tempCnt = 0;
    for (int i = 0; i < totalImgs;)
    {
        float x_axis = set x axis;
        int loopCount = 0;
        if (totalImgs - tempCnt >= (no of images in row))
        {
            loopCount = (no of images in row);
        }
        else
        {
            loopCount = totalImgs % (no of images in row);
        }
        for (int j = 0; j < loopCount; j++)
        {
            MasksData *mData = [masksList objectAtIndex:x];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(x_axis, y_axis, width, height);
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",mData.bgImage]] forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor  clearColor];
            btn.tag = x;
            [scroll View addSubview:btn];
            //photoCount++;
            x_axis += width + some space;
            tempCnt++;
            i++;
            x++;
        }
        y_axis += height + some space;
        scroll View.contentSize = CGSizeMake(320.0, y_axis);
    }
}

最新更新