【问题标题】:How to create UIScrollview inside UIScrollview programmatically如何以编程方式在 UIScrollview 中创建 UIScrollview
【发布时间】:2013-11-19 06:06:46
【问题描述】:

请告诉我如何在类似嵌套进程的滚动视图中设置滚动视图。

以下代码部分工作。

int x=10;
int y=10;


for(int i=0; i<5; i++)
{
    UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];
    scrollview.showsVerticalScrollIndicator=YES;
    scrollview.scrollEnabled=YES;
    scrollview.userInteractionEnabled=YES;
    scrollview.backgroundColor = [UIColor greenColor];
    [self.view addSubview:scrollview];
    scrollview.contentSize = CGSizeMake(50,50);
    y=y+95;
}

现在我只能看到 3 个滚动视图,而其他的都被隐藏了。如何创建主滚动视图以使子滚动视图不被隐藏?

【问题讨论】:

    标签: ios iphone ios6 uiscrollview ios7


    【解决方案1】:

    您需要有一个初始的滚动视图,然后将这些滚动视图放入其中。

    UIScrollView * mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    mainScrollView.contentSize = CGSizeMake(50, (y + 95) * 5);
    // further configure
    [self.view addSubview: mainScrollView];
    

    然后改变

    [self.view addSubview:scrollview];
    

    到

    [mainScrollView addSubview: scrollView];
    

    【讨论】:

    • 太棒了!很高兴它有帮助。您可以通过按复选标记将其标记为正确答案,以便其他人知道它是有效的吗?
    • 随时!祝你的项目好运。
    【解决方案2】:
    //I have created Two Scroll view programmatically this way
    UIScrollView *scrollViewOuter = [[UIScrollView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 600.0f, 600.0f)];
    scrollViewOuter.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    scrollViewOuter.contentSize = CGSizeMake(2000.0f, 2000.0f);
    
    UIScrollView *scrollViewInner = [[UIScrollView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 200.0f, 200.0f)];
    scrollViewInner.backgroundColor = [UIColor whiteColor];
    scrollViewInner.contentSize = CGSizeMake(2000.0f, 2000.0f);
    
    [scrollViewOuter addSubview:scrollViewInner];
    
    [self.window addSubview:scrollViewOuter];
    
    //You can change frame and use in your own way
    

    【讨论】:

      【解决方案3】:

      只需创建一个足够大的父滚动视图以容纳 5 个较小的滚动视图,然后更改此行:

      [self.view addSubview:scrollview];
      

      到

      [parentScrollView addSubview:scrollview];
      

      【讨论】:

        猜你喜欢
        • 2011-03-01
        • 2013-06-17
        • 2015-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-28
        • 2010-09-15
        • 1970-01-01
        相关资源
        最近更新 更多