UIScrollView表现得很奇怪

时间:2013-04-03 13:11:55

标签: ios cocoa-touch

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

这是我的代码

    -(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

1 个答案:

答案 0 :(得分:0)

如果包含滚动视图的视图控制器具有UINavigationControllerUITabBarController,则视图将自行调整大小以适应导航控制器的44个像素为724之后的边界,因此滚动视图可以滚动44像素。