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

发布于 2024-10-31 16:36:54 字数 388 浏览 0 评论 0原文

我在界面生成器中创建了 ScrollView 和 UIView (但也可以是代码)。我想向 UIView 添加随机数量的缩略图(子视图),并且如果超出屏幕可以容纳的数量,则能够上下滚动它。但是,我无法让 ScrollView 滚动。

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

什么时候设置scrollView的contentSize使其随着uiview展开?我尝试创建固定的大 contentSize 但效果不佳。

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

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

我想我也可以在没有 IB 的情况下在代码中创建两个视图。只是不知何故无法让它发挥作用。

提前致谢 !

I have ScrollView and UIView created in interface builder (but can as well be code). I want to add random amount of thumbnails (subviews) to UIView and be able to scroll it up-down if there are more than the screen can take. Hovewer, I can't get ScrollView to scroll.

Where and how do I resize UIView and add it as a subview of ScrollView?

When do I set contentSize of scrollView to make it expand along with uiview? I tried creating fixed large contentSize but didn't work as well.

What properties in IB I might need to change to make it work?

I'd like to make the images in uiview clickable in next step.

I guess I could also make both view in code without IB. Just somehow can't get it to work.

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋你朝朝暮暮 2024-11-07 16:36:54

//声明基本视图

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];

//声明滚动视图并添加到基本视图

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.width 是 320

//declare base view

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];

//declare scrollview and add to base view

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];

//Add your controls to scrollview that s image view or something which is needed to display

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

//took last control's y postion and set your K

float k=Last control's y position +100;(some thing which ll be decide scroll size)

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

use above line for scroll view size adjustment at run time.

screenSize.size.width is 320 here

甜点 2024-11-07 16:36:54

代码片段,

- (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);
    }
}

code snippet,

- (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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文