UIScrollView (paging mode) bounces only when there

2019-04-15 11:09发布

问题:

I just have this strange question. I have a UIScrollView, and I have only one page in that scroll. The scroll is paging enabled and bounce enabled.

Here is my code (in iPad)

scroll = [[UIScrollView alloc] init];
scroll.pagingEnabled = YES;
scroll.showsHorizontalScrollIndicator = NO;
scroll.showsVerticalScrollIndicator = NO;
scroll.scrollsToTop = NO;
scroll.bounces = YES;
scroll.delegate = self;
CGRect frame = CGRectMake(0.0, 0.0, 768, 1004);
scroll.frame = frame;
[self.view addSubview:scroll];


UIView *view1 = [[UIView alloc] init];
view1.frame = CGRectMake(0, 0.0, 768, 1004);
view1.clipsToBounds = YES;
view1.backgroundColor = [UIColor redColor];
[scroll addSubview:view1];


scroll.contentSize = CGSizeMake(768 * 1, 1004);

It is very simple. I just create one UIView, and add it to scroll. And set the scroll's contentSize to hold exact one view.

But after I run it, scroll does not bounce at all.

If I add 2nd View and set scroll's contentSize double Width, it bounces.

I am wondering whether scroll will never bounce if only one page in?

Thanks

回答1:

you can use the property :

scroll.alwaysBounceHorizontal= YES;

or

 scroll.alwaysBounceVertical = YES;

or both

Don't add 1px, it will create problems to you if you add touchable elements in the UIView inside your scrollview.

Never forget to define again your contentSize after redefine the frame of your scroll. even the contentSize keep the same value.

Good luck



回答2:

answer myself.

If you want UIScrollView to be able to bounce, you should set its contentSize bigger than its frame size.

Even 1px bigger is enough

for my own code, if I set scroll.contentSize = CGSizeMake(768 * 1 + 1, 1004); it will work and bounce.