UIScrollView 行为奇怪

Posted

技术标签:

【中文标题】UIScrollView 行为奇怪【英文标题】:UIScrollView behave strange 【发布时间】:2013-04-03 13:11:55 【问题描述】:

我将UIScrollView的subview的高度设置为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】:

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

【讨论】:

我在 Storyboard 中将状态栏和顶部栏设置为无。 @nimingzhe2008 你可能在情节提要中设置为 none,但是当你展示你的视图控制器时你实际上没有显示它吗?,滚动视图的状态栏是否在那里?,如果是这样将解释您可以滚动的 20 个像素。 谢谢您。状态实际上仍然存在。当我删除状态栏时,一切正常。

以上是关于UIScrollView 行为奇怪的主要内容,如果未能解决你的问题,请参考以下文章

UIView - UIScrollView 奇怪的行为

子类化 UIScrollView 时的奇怪行为

使用 UIScrollView 旋转后出现奇怪的行为

UIScrollView & UIButtons,真的很奇怪的行为

UIScrollView:内部视图不合适,奇怪的行为,视图在触摸时重新恢复

UIScroll 视图 + UIView + UITableview 问题