Image In Scroll View

寵の児 提交于 2019-12-23 22:13:58

问题


I am using the following code to try and display an image in a UIScrollView.

[self.jarView addSubview:imgView];

It does not work. However, it works when I add the subview to my normal view. My UIScrollView is called jarView and imgView is a UIImageView.

Thanks


回答1:


This works fine for me:

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

At last define how much scrollview is scroll:

scrollview.contentSize = CGSizeMake(width,height);



回答2:


Make sure that your scrollView.contentSize property value is set correctly (width and height should be > 0).




回答3:


Here I am posting my answer. Try it.

You can load all the images to an array. And you can design a view having one image view and try the below code:

array name: examplearray and view name :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);
}



回答4:


I am giving you my sample code. In this code i add multiple images on scroll view and this work fine for me:

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

I hope this helps.



来源:https://stackoverflow.com/questions/8260657/image-in-scroll-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!