UIScrollView (paging mode) bounces only when there two or more pages?

余生颓废 提交于 2020-01-01 19:31:33

问题


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.



来源:https://stackoverflow.com/questions/3716342/uiscrollview-paging-mode-bounces-only-when-there-two-or-more-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!