Scrollview无法通过编程方式创建

时间:2013-08-01 17:13:24

标签: ios uiscrollview

我想以编程方式创建一个scrollview,但是滚动视图没有创建以下是我的代码我想在该滚动视图中显示图像。

fscroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view1.frame.size.width, self.view1.frame.size.height)];
fscroll.contentSize = CGSizeMake(320, 400);
fscroll.backgroundColor = [UIColor blackColor];
fscroll.showsHorizontalScrollIndicator = YES;
[view1 addSubview:fscroll];

int X=0;
for (int i = 0; i < [images count]; i++)
{

    imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 320, 480)];
     imageView.backgroundColor = [UIColor blackColor];
     [imageView setImage: [images objectAtIndex:[sender tag]]];

    [imageView addSubview:fscroll];

    X = X + imageView.frame.size.height;

    if(X > 320)
        self.fscroll.contentSize = CGSizeMake(X, 134);

    if(X > 320)
        self.fscroll.contentSize = CGSizeMake(X, 134);
}

3 个答案:

答案 0 :(得分:0)

这是错误的:[imageView addSubview:fscroll];您想要做[fscroll addSubview:imageView];

答案 1 :(得分:0)

X = X + imageView.frame.size.height;

你的意思是这样做吗?

X = X + imageView.frame.size.width;

编辑:另外,你确定view1在初始化UIScrollView之前设置了正确的框架吗?

答案 2 :(得分:0)

简单方法:如果需要手段,可以多次创建

- (void)scrollView
{


    int x = 0; //scrollview width
    int y = 10;//scrollview height
    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);
        x=x+55;


    }
}