如何以编程方式在UIScrollview中创建UIScrollview

时间:2013-11-19 06:06:46

标签: ios iphone ios6 uiscrollview ios7

请告诉我如何在滚动视图中设置scrollview,如嵌套进程。

以下代码部分有效。

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个滚动视图,其他隐藏。如何创建主滚动以便查看子滚动视图?

3 个答案:

答案 0 :(得分:1)

您需要有一个初始scrollView,然后将这些scrollView放入。

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

答案 1 :(得分:1)

//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

答案 2 :(得分:0)

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

[self.view addSubview:scrollview];

[parentScrollView addSubview:scrollview];