简体   繁体   中英

Why UITableView can always scroll but UIScrollView can't

No matter what content size of UITableView , it can always scroll. but now I want UIScrollView also like it.....

At first, I use the code like this:

self.scrollView.contentSize = CGSizeMake(0, self.view.frame.size.height - 63);

It does work, but now I use AutoLayout, it said I must certain a content size.

the app have to adapt to iPhone4~iPhone 6 Plus, so the content size can't be certain, it always small than screen subtract 1, just like this (because the state bar and the navigation bar take over 64 px)

self.scrollView.contentSize = CGSizeMake(0, self.view.frame.size.height - 63);

BTW, if I don't certain a size, and write this code in - (void)viewDidLoad ,while Xcode compile it just a warming, don't affect the APP running.

I hope you can give me some better way.

我已经解决了这个问题,只需添加此scrollView.alwaysBounceVertical = YES ,然后无论内容是否超出框架,我都可以滚动ScrollView

First of all, I barely understand what you want.

Now the technical part:

  1. It's not a good practice, hell, you should never change the content size of a UITableView , UITableView always compute his content size based on heightFirRowAtIndexPath , heightForHeader... and the other heightFor... methods, that's why the table will scroll (of course if the methods are implemented right)

  2. The UIScrollview won't compute the content size by itself because it's not designed that way, so you'll have to specify the content size for it. Usually when a UIScrollView doesn't scroll it's because the contentSize it's smaller than the scroll's frame size so it doesn't have to scroll.

  3. Implementing a UIScrollView with auto-layout it's a little tricky and you have to do some tweaks, but Apple provides a some examples here how you should do it. Also a little googling will find a ton of examples on how to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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