无法在scrollview中看到图像

时间:2012-11-28 05:15:51

标签: iphone objective-c uiscrollview uiimageview

我有2 scrollviews和1 imageview

现在在主要内部scrollview我还有另一个scrollview,在此scrollview我有图像。

所以,我的主Scrollview名为“ScrollView”,而子scrollview名为“scrView

现在我可以在“scrView”中添加“ScrollView”,但我无法在“scrView”中看到我的图片

我的编码如下:

int numberOfPages = 10;
    [scrollView setPagingEnabled:YES];
    [scrollView setContentSize:CGSizeMake(scrollView.bounds.size.width * numberOfPages, scrollView.bounds.size.height)];

    CGRect pageFrame;
    CGRect imageFrame;
    UIImageView *imageView;

    for(int i=0; i < numberOfPages; i++)
    {
        pageFrame = CGRectMake(i * scrollView.bounds.size.width+50, 0.0f, scrollView.bounds.size.width-100, scrollView.bounds.size.height);
        UIScrollView *scrView = [[UIScrollView alloc] init];
        imageFrame = CGRectMake(i * scrView.bounds.size.width, 0.0f, scrView.bounds.size.width, scrView.bounds.size.height);
        imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.jpg"]];
                    [scrView setFrame:pageFrame];
        [imageView setFrame:imageFrame];
        [scrView setBackgroundColor:[UIColor redColor]];

        [scrollView addSubview:scrView];
        [scrView addSubview:imageView];

        [imageView release];
    }

如果您查看代码,我已将imageView添加为subView scrView,但我无法在模拟器中看到图像。

问题是什么?

与我分享你的想法。

感谢...

3 个答案:

答案 0 :(得分:2)

这是有用的尝试此代码。问题在于您的imageview框架

int numberOfPages = 10;
[scrollView setPagingEnabled:YES];
[scrollView setContentSize:CGSizeMake(scrollView.bounds.size.width * numberOfPages, scrollView.bounds.size.height)];

CGRect pageFrame;
CGRect imageFrame;
UIImageView *imageView;

for(int i=0; i < numberOfPages; i++)
{
    pageFrame = CGRectMake(i * scrollView.bounds.size.width+50, 0.0f, scrollView.bounds.size.width-100, scrollView.bounds.size.height);
    UIScrollView *scrView = [[UIScrollView alloc] initWithFrame:pageFrame];
    imageFrame = CGRectMake(0.0, 0.0f, scrView.bounds.size.width, scrView.bounds.size.height);

    imageView = [[UIImageView alloc] initWithFrame:imageFrame];
    imageView.image=[UIImage imageNamed:@"image1.jpg"];

   // [scrView setFrame:pageFrame];

    [scrView setBackgroundColor:[UIColor redColor]];
    [scrView addSubview:imageView];
    [scrollView addSubview:scrView];


    [imageView release];
}

答案 1 :(得分:0)

背景颜色设置为 imageView 。然后记录并检查scrView&amp; imageFrame 。还可以使用 bringSubviewToFront: 属性。

for(int i=0; i < numberOfPages; i++)
    {
        UIScrollView *scrView = [[UIScrollView alloc] init];
        [scrView setBackgroundColor:[UIColor redColor]]; 
        pageFrame = CGRectMake(i * scrollView.bounds.size.width+50, 0.0f, scrollView.bounds.size.width-100, scrollView.bounds.size.height);

        imageFrame = CGRectMake(i * scrView.bounds.size.width, 0.0f, scrView.bounds.size.width, scrView.bounds.size.height);
        imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.jpg"]];
                    [scrView setFrame:pageFrame];
        [imageView setFrame:imageFrame];
        [imageView setBackgroundColor:[UIColor blueColor]]
        [scrView addSubview:imageView];
        [scrView bringSubviewToFront:imageView];
        [imageView release];

        [scrollView addSubview:scrView];
        [scrollView bringSubviewToFront:scrView];
    }

答案 2 :(得分:0)

尝试从您的代码配对设置这样..

[scrView setBackgroundColor:[UIColor clearColor]];
[scrView addSubview:imageView]; /// first add imageview and then scrview (screen view) to scrollview
[scrollView addSubview:scrView];