iPhone页面控件仅显示在UIScrollView的第一页上

时间:2011-07-10 15:15:59

标签: iphone objective-c uiscrollview uipagecontrol pager

我的页面控件仅显示在UIScrollView的第一页上。滚动到下一页后,它就会消失。

你可以告诉我我做错了什么吗?因为每次向UIScrollView添加子视图时,我都很想知道是否需要添加页面控件的子视图。

我的相关代码:

-(IBAction)clickPageControl:(id)sender
{
    int page=pageControl.currentPage;
    CGRect frame=scroller.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [scroller scrollRectToVisible:frame animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int page = scrollView.contentOffset.x/scrollView.frame.size.width;
    pageControl.currentPage=page;
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    arrayCount = [array count];
    scroller.delegate=self;
    scroller.pagingEnabled=YES;
    scroller.directionalLockEnabled=YES;
    scroller.showsHorizontalScrollIndicator=NO;
    scroller.showsVerticalScrollIndicator=NO;

    //should have an array of photo objects and the number of objects, correct?
    scrollWidth = 0;
    scroller.contentSize=CGSizeMake(arrayCount*scroller.frame.size.width, scroller.frame.size.height);

    for (int i = 0; i < arrayCount;i++) {
        PhotoViewController *pvc = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];        
        UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:CGRectOffset(scroller.bounds, scrollWidth, 0)];
        CGRect rect = scrollImageView.frame;
        pvc.view.frame  = rect;
        [pvc view];
        pvc.label.textColor = [UIColor whiteColor];
        id individualPhoto = [array objectAtIndex:i];
        NSLog(@"%@",individualPhoto);
        NSArray *keys=[individualPhoto allKeys];
        NSLog(@"%@",keys);
        NSString *imageURL=[individualPhoto objectForKey:@"source"];
        //here you can use this imageURL to get image-data and display it in imageView  
        NSURL *url = [NSURL URLWithString:imageURL];
        NSData  *data = [NSData dataWithContentsOfURL:url];
        pvc.imageView.image = [[UIImage alloc] initWithData:data];
        pvc.label.text = [NSString stringWithFormat:@"Photo Number: %i", arrayCount];
        //check to make sure the proper URL was passed
        //I have an imageView next to the UIScrollView to test whether that works - it does.
        [scroller addSubview:pvc.view];
        [scrollImageView release];
        [pvc release];
        scrollWidth += scroller.frame.size.width;
    }

    if (arrayCount > 3) {
        pageControl.numberOfPages=3;
    } else {
    pageControl.numberOfPages=arrayCount;
    }
    pageControl.currentPage=0;
    [self.view addSubview:scroller];
}

1 个答案:

答案 0 :(得分:4)

您不应将页面控件添加为滚动视图的子视图,这会导致它与内容一起滚动。改为使其成为滚动视图的兄弟。