繁体   English   中英

UIView内的UIScrollView

[英]UIScrollView inside UIView

我在一个项目中,我在UIView中有一个UIScrollView。 在UIScrollView内部必须查看3列和X行的按钮列表。 我有显示按钮的代码,但是没有插入到UIScrollView中,它会打印并且不会滚动。 除此之外,还要覆盖视图底部的TabBar。

显示按钮的功能代码为

for (int y_axis=0; y_axis<=3; y_axis++)
    {
        for (int x_axis=0; x_axis<=2; x_axis++)
        {

            UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(16+100*x_axis,100.0+115*y_axis,88.0 ,88.0)];
            btn.backgroundColor=[UIColor brownColor];
            [self.view addSubview:btn];

        }
    }

如何在UIScrollView中显示此内容? 谢谢!!

假设您有一个UIScrollView滚动器,则必须将最后一行更改为此。

[self.scroller addSubview:btn];

如果不是,则将其添加到视图中,而不是滚动器中。 此外,还必须更改滚动条的contentsize,如果包含多少则为X * Y。 它包含整个屏幕,仅此而已;

[self.scroller setContentSize:CGSizeMake(320, 480)]
                                         ^     ^

将其替换为要滚动的宽度和高度。 每次在新按钮下方添加一个按钮时,都应更新contentsize。

这是因为您要在视图[self.view addSubview:btn];上添加按钮[self.view addSubview:btn]; 不在ScrollView 尝试使用所有按钮进行查看。 并设置有按键为您的视图contentview您的scrollview

首先,您需要在.h文件中使用UIScrollViewDelegate ,然后还检查按钮是否为ScrollView的子视图,也可以使用以下代码在滚动视图中添加此按钮...

[yourScrollView addSubview:yourbuttonname];

之后,在您的viewDidLoad方法中像这样声明您的scrollview的contentsize ...

yourScrollView.contentSize=CGSizeMake(320, anyheight);///here define height which you want to scroll the view...

希望对您有帮助.... :)

请尝试以下步骤:

    for loop
    {
        create buttons along with the desired frames;
        add those buttons to scrollview;
    }
    set the content size of the scrollview according to no of rows;
    add the scroll view to main view i.e. self.view;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM