UIScrollView setting page size dynamically

10,786

Solution 1

I think you're confused about contentSize vs the paging size. The paging size is always the size of the scrollView bounds, and isn't a property you can otherwise set. That is, when you swipe left/right it will "page" by the width of the scrollview.

contentSize is the size of the virtual bounding box of all the subviews within the scrollview. This only serves to limit how far the scrollview will scroll, and for paging, how many times it will page, i.e. contentSize.width / bounds.size.width.

Assuming the scrollView isn't zoomed in/out (zoomScale = 1.0) then you need to position and size your subviews on the virtual 'page boundaries'. They can take up the full page boundary (be sized to match scrollview.bounds) or be inset. If you have some content that is larger/smaller then you'll have to decide if you want to change the scale of that content or size it up/down within the page bounds.

Solution 2

please, try to get the actual content size from the current content.

the content must be some inherited class from the UIView and it has a frame.size.width and frame.size.height property.

you can use those to set the contentSize of your UIScrollview for the current content at that time when you add the content to the UIScrollView.

Solution 3

Yes you can dynamically set the content size of the scrollView. Also You can use this method:

self.scrollViewNews.contentSize = CGSizeMake(constant1,constant2); 

Nothing wrong this method. You are seeing the blank space at bottom because you ares setting the height of the scrollView's contentSize to a larger value than it's content. That's the issue. Adjust the height according to the contents, blank space will go.

Share:
10,786
limon
Author by

limon

Keen interest in mobile development. I like blocks. SOreadytohelp

Updated on June 27, 2022

Comments

  • limon
    limon almost 2 years

    I've done quite a bit of looking into this but have been unsuccessful in finding a solution.

    I have a UIScrollView called scrollView inside of UIView. I'm using this scrollView in pagingEnabled mode. Inside of scrollView, I have 3 different views created programmatically. Inside of these 3 different views, I have a bunch of stuff(UILabel,UITextView etc.). All of the views contents are dynamic and determined at runtime.So, i really don't know scrollView.contentSize. If i give the content size of this scrollView,Sometimes ,I have a screen with white blank at the botton of the screen when user scrolls down. My question is : Can i set the content size dynamically for each single page of this scrollView? For example,for page 1 :

    self.scrollViewNews.contentSize = CGSizeMake(constant1,constant2);

    And set something else for page 2 as well .

  • limon
    limon over 11 years
    With this method,i'm not able to change content size for a single page.This method makes all of the page's content size constant1,constant2.I want page1 and page2 have different content size. So,i cannot use this method.
  • limon
    limon over 11 years
    It's not what i'm looking for either.Take a look at a sample app,all of the pages have the same size.scroller.contentSize = CGSizeMake(1650, 2000); this is what i use for now,but as u imagine,if the content size smaller than 1650,2000 which is the most case,user is going to see the white blank space at the bottom of the screen.Do you understand what i mean ?
  • Shamsudheen TK
    Shamsudheen TK over 11 years
    oh.. better make sure to set the contentSize is always larger than the scrollView frame. Else please reset the scrollView Frame (smaller than contentSize) again before going to set the contentSize . please do this and what about the method scrollRectToVisible method ??