2013-04-03 103 views
0

我把UIScrollView的子视图的高度设置为768,而UIScrollView的contentSize是1024 * 768。但是当我向上滑动或向下滑动时,子视图(UIImageView)会向上或向下滚动一点。我不知道为什么。UIScrollView表现得很奇怪

这里是我的代码

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // scrollView is an outlet of UIScrollView 
    [scrollView setBackgroundColor:[UIColor blackColor]]; 
    [scrollView setCanCancelContentTouches:NO]; 
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
    scrollView.clipsToBounds = YES;  // default is NO, we want to restrict drawing within our scrollview 
    scrollView.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. 
    scrollView.pagingEnabled = YES; 

    // load all the images from our bundle and add them to the scroll view 
    NSUInteger i; 
    imageCount=3; 
    for (i = 1; i <= imageCount; i++) 
    { 
     NSString *imageName = [NSString stringWithFormat:@"tu%d_s.png", i]; 
     UIImage *image = [UIImage imageNamed:imageName]; 
     if(!image) 
     { 
      NSLog(@"%@ is nil",imageName); 
     } 
     UIImageView *TempImageView = [[UIImageView alloc] initWithImage:image]; 

     // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
     CGRect rect = TempImageView.frame; 
     rect.size.height = 768; 
     rect.size.width = 1024; 
     TempImageView.frame = rect; 
     TempImageView.tag = i; // tag our images for later use when we place them in serial fashion 
     [scrollView addSubview:TempImageView]; 
     NSLog(@"viewDidLoad TempImageView width %f,height %f,x %f,y %f",TempImageView.frame.size.width,TempImageView.frame.size.height,TempImageView.frame.origin.x,TempImageView.frame.origin.y); 

    } 

    [self layoutScrollImages]; // now place the photos in serial layout within the scrollview 

} 

- (void)layoutScrollImages 
{ 
    UIImageView *view = nil; 
    NSArray *subviews = [scrollView 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 += (1024); 
     } 
     NSLog(@"shang2 layoutScrollImages width %f,height %f,x %f,y %f",view.frame.size.width,view.frame.size.height,view.frame.origin.x,view.frame.origin.y); 
    } 

    // set the content size so it can be scrollable 
    [scrollView setContentSize:CGSizeMake((imageCount * 1024), 768)]; 
    NSLog(@"shang2 content width %f,height %f,x %f,y %f",scrollView.contentSize.width,scrollView.contentSize.height,scrollView.frame.origin.x,scrollView.frame.origin.y); 
} 

这里是Xcode的日志

2013-04-03 20:50:23.939 animation[1464:c07] viewDidLoad TempImageView width 1024.000000,height 768.000000,x 0.000000,y 0.000000 
2013-04-03 20:50:23.952 animation[1464:c07] viewDidLoad TempImageView width 1024.000000,height 768.000000,x 0.000000,y 0.000000 
2013-04-03 20:50:23.995 animation[1464:c07] viewDidLoad TempImageView width 1024.000000,height 768.000000,x 0.000000,y 0.000000 
2013-04-03 20:50:23.995 animation[1464:c07] shang2 layoutScrollImages width 7.000000,height 5.000000,x 741.000000,y 1019.000000 
2013-04-03 20:50:23.996 animation[1464:c07] shang2 layoutScrollImages width 1024.000000,height 768.000000,x 0.000000,y 0.000000 
2013-04-03 20:50:23.996 animation[1464:c07] shang2 layoutScrollImages width 1024.000000,height 768.000000,x 1024.000000,y 0.000000 
2013-04-03 20:50:23.996 animation[1464:c07] shang2 layoutScrollImages width 1024.000000,height 768.000000,x 2048.000000,y 0.000000 
2013-04-03 20:50:23.996 animation[1464:c07] shang2 content width 3072.000000,height 768.000000,x 0.000000,y 0.000000 

回答

0

如果包含了滚动视图控制器设有一张UINavigationControllerUITabBarController,视图将调整自身以适应范围哪些在来自导航控制器的44个像素将是724之后,因此滚动视图可以滚动44个像素。

+0

我在故事板中将状态栏和顶部栏设置为无。 –

+0

@ nimingzhe2008你可能在storyboard中没有任何设置,但是当你展示你的视图控制器时,你实际上没有展示它吗?是那里的scrollview?的状态栏,如果这样可以解释你的20个像素可以滚动。 –

+0

谢谢。状态实际上仍然存在。当我删除状态栏时,一切正常。 –