0

我制作了一个 UIScrollView 并显示来自 Array 的图像,但图像未显示

这是我的代码

- (void)viewDidLoad
{
    [super viewDidLoad];

imageScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(13.0, 50.0, 292.0, 69.0)];

    [self makeScroll];

}

-(void)makeScroll
{
    imageScroll.delegate = self;

    imageScroll.backgroundColor = [UIColor clearColor];

    int arrayCount = [myArray count];

    imageScroll.contentSize = CGSizeMake(arrayCount * 101.0, 69.0);

    for (int i = 0; i < arrayCount; i++)

    {

        participantView = [[UIView alloc]init];

        if (i == 0) {

            participantView.frame = CGRectMake(0.0, 2.0, 91.0, 67.0);

        }

        else {

            participantView.frame = CGRectMake(101.0*i, 2.0, 91.0, 67.0);

        }

        participantView.backgroundColor = [UIColor yellowColor];

        //declaring the view in which image object will be added
        UIImageView *bannerImage = [[UIImageView alloc] 

initWithFrame:CGRectMake(0.0,0.0,91.0,67.0)];

        //taking out the image object
        UIImage *imageNamed = [UIImage imageNamed:[myArray objectAtIndex:0]];

        //resizing the image

   // UIImage *imageNamed2 = [imageNamed 

imageByScalingAndCroppingForSize:CGSizeMake(91.0,67.0)];

        //setting the resized image in UIImageView

        [bannerImage setImage:imageNamed];

        //clear the bg
        bannerImage.backgroundColor = [UIColor clearColor];

        bannerImage.autoresizingMask = UIViewAutoresizingFlexibleHeight;

        bannerImage.contentMode = UIViewContentModeScaleAspectFit;

        [bannerImage setNeedsLayout];

        bannerImage.frame = CGRectMake(0.0,0.0,91.0,67.0);

    [participantView addSubview:bannerImage];



            [imageScroll addSubview:participantView];

    }

    [self.view addSubview:imageScroll];


}

谁能告诉我我在哪里做错了

问题是:图像未在滚动视图中显示

4

7 回答 7

2

图像未显示,因为您将图像添加为 UIView 的子视图。

牢记层次结构。

首先添加图像作为滚动视图的子视图,然后添加滚动视图作为 UIView 的子视图。

或者,如果您使用情节提要,则可以直接从那里设置层次结构。

于 2013-06-27T07:07:58.150 回答
0

参考以下代码。这对我有用:

            scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,50, 768, 40)];
            int scrollWidth=60;
            for (int i=0;i<array.count;i++)
            {
                UIImageView *imageView1=[[UIImageView alloc]initWithFrame:CGRectMake(scrollWidth,8,50,40)];
                imageView1.image=[array objectAtIndex:i];
                [scrollView addSubview:imageView1];
                scrollWidth=scrollWidth+80;
            }
            [scrollView setContentSize:CGSizeMake(scrollWidth, 30)];
            [self.view addSubview:scrollView];
于 2013-06-27T07:19:16.130 回答
0
   scrl_venuelist.delegate=self;
    scrl_venuelist.contentSize =CGSizeMake(273 * [imageArr count], 137);

    int scroll_x=0;
    for (int i=0; i < [imageArr count]; i++)
    {
        UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(scroll_x, 0, 100, 100)] autorelease];
        img.image =[UIImage imageNamed:[[imageArr objectAtIndex:i] objectForKey:@"ImgName"]];
        [scrl_venuelist addSubview:img];
        scroll_x = scroll_x + 273;
    }
}
于 2013-06-27T07:13:22.840 回答
0

尝试这个 -

设置页数

    static NSUInteger numberOfPages = [imageArr count];

在 ViewDidLoad

    howItWorksScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, 320, 640)];
    howItWorksScrollView.pagingEnabled = YES;
    howItWorksScrollView.contentSize = CGSizeMake(howItWorksScrollView.frame.size.width * numberOfPages, howItWorksScrollView.frame.size.height);
    howItWorksScrollView.showsHorizontalScrollIndicator = YES;
    howItWorksScrollView.showsVerticalScrollIndicator = NO;
    howItWorksScrollView.scrollsToTop = NO;
    howItWorksScrollView.delegate = self;
    [self.view addSubview:howItWorksScrollView];

    int k,i;
    int btnX = 70, btnY = 25;

    int count =0;


    for (k = 0; k < numberOfPages; k++)
    {
        for (i = 0; i < 1; i++)
        {
            if (count < [imageArray count])
            {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(btnX, btnY, 180, 180)];
                imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[imageArray objectAtIndex:k]]];
                imageView.backgroundColor = [UIColor clearColor];
                [howItWorksScrollView addSubview:imageView];
                [imageView release];

                count++;

                btnX = btnX + 180 + 70 + 70;

            }

            btnX = btnX - 320;

        }
        btnX = btnX + 320;
        btnY = 25;

    }
于 2013-06-27T07:24:12.037 回答
0

看看 VSScrollview。它是一个非常简单易用的类。它的使用方式与 UITableview 的使用方式相同,就像 UITableview 重用其单元格一样,VSScrollView 重用其视图,因此当您的 Scrollview 上有大量子视图时,您不必担心处理内存。VSScrollview 链接

于 2013-06-27T07:24:41.487 回答
0

Nicklockwood的 SwipeView 是一个很好的库,用于处理水平滚动视图中的视图。

从文档:

SwipeView 是一个类,旨在简化 iOS 上水平、分页滚动视图的实现。它基于 UIScrollView,但增加了方便的功能,例如用于动态加载视图的 UITableView 样式的 dataSource/delegate 接口,以及高效的视图加载、卸载和回收。

SwipeView 的界面和实现是基于 iCarousel 库的,任何使用过 iCarousel 的人都应该熟悉。

于 2014-04-26T08:37:32.030 回答
-2

感谢朋友帮助我

我从哪里弄错了问题,

在我的数组中,图像在 URL 中,所以我做了这个并且它的工作

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0f, 91.0f, 67.0f)];

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: [myArray objectAtIndex:i]]];

UIImage *img = [[UIImage alloc] initWithData:data];

[imageView setImage:img];

问题解决了 :-)

于 2013-06-27T07:41:17.773 回答