2011-11-24 60 views
2

我正在使用以下代码尝试在UIScrollView中显示图像。滚动视图中的图像

[self.jarView addSubview:imgView]; 

它不起作用。但是,它在我将子视图添加到普通视图时起作用。我的UIScrollView被称为jarView,而imgView是一个UIImageView。

感谢

+0

如果它与普通视图的工作,那么问题出在'self.jarView '。检查你的财产,出口,初始化方法。 – beryllium

+0

@beryllium什么是init方法? ScrollView是否需要初始化?我的图像已正确初始化。滚动视图是在Interface Builder中创建的。 –

+0

scrollview是否在界面构建器中链接到IBOutlet jarView? –

回答

1

也能正常工作对我来说:

UIScrollView *scrollview; 
scrollview= [[UIScrollView alloc] initWithFrame:CGRectMake(,,,)];// set scroll view size 
view = [[UIView alloc] initWithFrame:CGRectMake(y1,x1,y2,x2)]; //set the view size 
UIImage *image1 = [UIImage imageNamed:imageName]; 
image=[[UIImageView alloc]initWithImage:image1];// take image size according to view 
[view addSubview:image]; 
[scrollview addSubview:view]; 
[view release]; 

最后确定scrollview多少是滚动:

scrollview.contentSize = CGSizeMake(width,height); 
+0

2问题:1)是ScrollView和View,新的还是他们现有的IB对象? 2)y1,x1,y2,x2与ScrollView相同吗?谢谢 –

+0

在这里我做所有动态编程......我定义scrollview ..并为它分配内存.... 2)y1,x1,y2,x2坐标我从滚动视图少一点(所以用户可以滚动) .....但(查看高度=图像高度)...如果这篇文章对你有帮助....不要忘记接受我的回答....你永远欢迎..:) – GauravBoss

+0

我有多个图像,单行代码是在for循环。它仍然有效吗?(图像不重叠) –

0

确保您scrollView.contentSize属性值设置正确(宽度并且高度应该> 0)。

+0

它不起作用。 。 。 –

0

我给你我的示例代码。在这段代码中,我在滚动视图中添加了多个图像,并且这项工作适合我:

UIScrollView *scrollview; 

- (void)anyFunction 
{ 
    scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, 50)]; 
    NSInteger viewcount = [cat_Products count]; // if you have four content your view count is 4..(four)..in my case i fetch it from database 

    CGFloat y2 = 0; 
    UIView *view; 
    UIImageView *imageView; 
    NSString *imageName; 

    for(int i = 0; i< viewcount; i++) 
    { 
     CGFloat y = i * 64; 
     y2 = y2 + 64; 

     view = [[UIView alloc] initWithFrame:CGRectMake(y, 0, y2, 50)]; 

     imageName = [[NSString alloc] initWithFormat:@"c%i", (i + 1)]; // my image name is c1, c2, c3, c4 so i use this 

     UIImage *image1 = [UIImage imageNamed:imageName]; 
     imageView = [[UIImageView alloc] initWithImage:image1]; 
     [view addSubview:imageView]; 
     [scrollview addSubview:view]; 
     [view release]; 
     [imageView release]; 
     [imageName release]; 
    }  

scrollview.contentSize = CGSizeMake(viewcount*64,0); //viewcount*64 because i use image width 64..and second parameter is 0..because i not wants vertical scrolling... 
[myView addSubview:scrollview]; 
[scrollview release]; 
} 

我希望这有助于。

0

在这里,我张贴我的答案。尝试一下。

您可以将所有图像加载到数组中。你也可以设计具有一个图像视图中的视图,并尝试下面的代码:

数组名称:examplearray和视图名称:exampleview

-(void)updateImagesInScrollView 
{ 
    int cnt = [examplearray count]; 

    for(int j=0; j< cnt; ++j) 
    { 
     NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"exampleview" 
                  owner:self 
                  options:nil]; 

     UIView *myView = [nibContents objectAtIndex:0]; 
     exampleview * rview= (exampleview *)myView; 
     /* you can get your image from the array and set the image*/ 
     rview.imageview.image = yourimage; 
     /*adding the view to your scrollview*/ 
     [self.ScrollView addSubview:rview]; 
    } 
    /* you need to set the content size of the scroll view */ 
    self.ScrollView.contentSize = CGSizeMake(X, self.mHorizontalScrollView.contentSize.height); 
}