0

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

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);
}
4

3 回答 3

0

这是错误的:[imageView addSubview:fscroll];假设你想做[fscroll addSubview:imageView];

于 2013-08-01T17:21:05.303 回答
0
X = X + imageView.frame.size.height;

你的意思是这样做吗?

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

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

于 2013-08-01T17:56:36.343 回答
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;


    }
}
于 2013-11-14T13:34:04.277 回答