【问题标题】:UIScrollView inside UIViewUIView里面的UIScrollView
【发布时间】:2012-05-14 18:04:03
【问题描述】:

我有一个项目,其中我在 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 中显示它?谢谢!!

【问题讨论】:

    标签: iphone objective-c ios xcode


    【解决方案1】:

    假设你有一个 UIScrollView 滚动条,你必须把你的最后一行改成这个;

    [self.scroller addSubview:btn];
    

    如果没有,您将其添加到视图中,而不是滚动条。此外,您必须更改滚动条内容大小,如果它包含多少,则为 X*Y。让它包含整个屏幕,仅此而已;

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

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

    【讨论】:

    • 它有效,但在您的第一行将 self.scroller 修改为滚动条。谢谢!
    【解决方案2】:

    这是因为您在视图 [self.view addSubview:btn]; 而不是 ScrollView 上添加按钮。尝试使用所有按钮创建视图。并使用按钮将视图设置为 contentviewscrollview

    【讨论】:

      【解决方案3】:

      首先,您需要在 .h 文件中添加 UIScrollViewDelegate,然后还要检查按钮是否是 yourScrollView 的子视图,或者您是否在滚动视图中添加此按钮,代码如下所示...

      [yourScrollView addSubview:yourbuttonname];
      

      之后在你的 viewDidLoad 方法中声明你的滚动视图的 contentsize 像这样......

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

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

      【讨论】:

        【解决方案4】:

        试试这些步骤:

            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;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-11
          • 2013-04-24
          • 2013-03-04
          • 2013-02-28
          • 1970-01-01
          • 1970-01-01
          • 2013-01-30
          相关资源
          最近更新 更多