简体   繁体   English

UIView内的UIScrollView

[英]UIScrollView inside UIView

I'm with a project in which I have a UIScrollView inside a UIView. 我在一个项目中,我在UIView中有一个UIScrollView。 Inside the UIScrollView has to see a list of buttons in 3 columns and X rows. 在UIScrollView内部必须查看3列和X行的按钮列表。 I have the code that displays the buttons, but does not inserted into the UIScrollView, it prints over and does not scroll. 我有显示按钮的代码,但是没有插入到UIScrollView中,它会打印并且不会滚动。 Besides this, also cover the TabBar I have in the bottom of the view. 除此之外,还要覆盖视图底部的TabBar。

The code of the function that displais the buttons is 显示按钮的功能代码为

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

        }
    }

How can I do to display this in the UIScrollView? 如何在UIScrollView中显示此内容? Thanks!! 谢谢!!

Assuming you have a UIScrollView scroller, you have to change your last line to this; 假设您有一个UIScrollView滚动器,则必须将最后一行更改为此。

[self.scroller addSubview:btn];

If not, you are adding it to the view, and not the scroller. 如果不是,则将其添加到视图中,而不是滚动器中。 Also, you have to change the scrollers contentsize, which is the X*Y if how much it contains. 此外,还必须更改滚动条的contentsize,如果包含多少则为X * Y。 For it to contain the whole screen and nothing more; 它包含整个屏幕,仅此而已;

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

Replace that with the width and the height you want to scroll. 将其替换为要滚动的宽度和高度。 Each time you add a button below your new buttons, you should update the contentsize. 每次在新按钮下方添加一个按钮时,都应更新contentsize。

It's because you are adding buttons on view [self.view addSubview:btn]; 这是因为您要在视图[self.view addSubview:btn];上添加按钮[self.view addSubview:btn]; not on ScrollView . 不在ScrollView Try to make a view with all you buttons. 尝试使用所有按钮进行查看。 and set your view with buttons as contentview of your scrollview . 并设置有按键为您的视图contentview您的scrollview

first you need the UIScrollViewDelegate in your .h file and then also check buttons are subview of yourScrollView or not also you add this button in your scrollview with code like this bellow... 首先,您需要在.h文件中使用UIScrollViewDelegate ,然后还检查按钮是否为ScrollView的子视图,也可以使用以下代码在滚动视图中添加此按钮...

[yourScrollView addSubview:yourbuttonname];

after that in your viewDidLoad method declare contentsize of your scrollview like this... 之后,在您的viewDidLoad方法中像这样声明您的scrollview的contentsize ...

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

hope this help you.... :) 希望对您有帮助.... :)

Try these steps: 请尝试以下步骤:

    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