简体   繁体   English

滚动视图上不需要的子视图反弹

[英]Unwanted bounce of subviews on scrollview

I have a scrollview that acts as a banner with 15 image views as subviews (scrolled horizontally). 我有一个滚动视图,它充当带有15个图像视图的横幅作为子视图(水平滚动)。 I add the subviews this way: 我以这种方式添加子视图:

for (int i = 0; i < featuredImages.count; i++) {

    CGRect frame;
    frame.origin.x = self.scrollViewFeaturedImages.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollViewFeaturedImages.frame.size;

    UIImageView *subview = [[UIImageView alloc] init];
    subview.frame = frame;
    [self.scrollViewFeaturedImages addSubview:subview];
}

And set the contentSize accordingly: 并相应地设置contentSize:

self.scrollViewFeaturedImages.contentSize = CGSizeMake(self.scrollViewFeaturedImages.frame.size.width * featuredImages.count, self.scrollViewFeaturedImages.frame.size.height);

self.scrollViewFeaturedImages.contentInset = UIEdgeInsetsZero;

However, when the view appears, the first image seems to be a little off. 但是,当视图出现时,第一张图像似乎有点偏离。

图像框和滚动框之间的空间

When I scroll (horizontally) to the next image, the gap disappears, and when I scroll back to the first image, the frame is corrected already. 当我(水平)滚动到下一张图像时,间隙消失,而当我滚动回第一张图像时,该帧已经被校正。

I've also disabled BOUNCE but I can drag the image vertically. 我也禁用了BOUNCE,但是我可以垂直拖动图像。

在此处输入图片说明

I've also tried this: 我也尝试过这个:

self.automaticallyAdjustsScrollViewInsets = NO;

with no success. 没有成功。

What's going on? 这是怎么回事?

After being puzzled for 2 weeks I finally got this fixed by adding these lines in viewDidAppear . 经过2个星期的困惑,我终于通过在viewDidAppear添加以下行来解决此问题。

self.scrollViewFeaturedImages.contentInset = UIEdgeInsetsZero;
self.scrollViewFeaturedImages.scrollIndicatorInsets = UIEdgeInsetsZero;

您是否尝试将UIImageView的属性contentMode更改为UIViewContentModeScaleToFill

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM