为什么图像没有显示在UIScrollView中?

时间:2012-08-17 07:58:12

标签: objective-c ios

我正在使用此代码显示滚动图像,但我只是获得了白色屏幕。我拖放uiscrollview,创建了iboutlet,合成它,其余代码在这里

@synthesize scrollView1;

const CGFloat kScrollObjHeight  = 199.0;
const CGFloat kScrollObjWidth   = 280.0;
const NSUInteger kNumImages     = 5;

- (void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subviews = [scrollView1 subviews];

    // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = 0;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(curXLoc, 0);
            view.frame = frame;

            curXLoc += (kScrollObjWidth);
        }
    }

    // set the content size so it can be scrollable
    [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    scrollView1 = [[UIScrollView alloc]init];

    bgArray = [NSArray arrayWithObjects:@"Bg", @"Bg1.png", @"Bg2.png", @"Bg3.png", @"Bg4.png", @"Bg5.png", @"Bg6.png", @"Bg7.png", @"Bg8.png", nil];

    [scrollView1 setBackgroundColor:[UIColor blackColor]];
    [scrollView1 setCanCancelContentTouches:NO];
    scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView1.clipsToBounds = YES;        // default is NO, we want to restrict drawing within our scrollview
    scrollView1.scrollEnabled = YES;

    // pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
    // if you want free-flowing scroll, don't set this property.
    scrollView1.pagingEnabled = YES;

    // load all the images from our bundle and add them to the scroll view
    NSUInteger i;
    for (i = 0; i < [bgArray count]; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"%@", [bgArray objectAtIndex:i]];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
        CGRect rect = imageView.frame;
        rect.size.height = 199.0;
        rect.size.width = 280.0;
        imageView.frame = rect;
        imageView.tag = i;  // tag our images for later use when we place them in serial fashion
        [scrollView1 addSubview:imageView];
        [scrollView1 setShowsHorizontalScrollIndicator:NO];
        [scrollView1 setShowsVerticalScrollIndicator:NO];
    }

    [self layoutScrollImages];

}

0 个答案:

没有答案