Centering UIScrollView element

Discussion in 'iOS Programming' started by arnieterm, Jan 19, 2010.

  1. arnieterm macrumors regular

    Joined:
    Aug 28, 2008
    #1
    Hi All,
    I have a UIScrollView that contains a series of UIView in its content view. The scrollview builds as desired and scrolls well with its content size set as needed. I cannot use PagingEnabled==YES due to design restrictions. At any given time the scrollview shows five UIView. When user scrolls enough [== scrollview's bounds] then another 5 UIView appears.
    My problem is that I need to place the third UIView [in the currently visible 5 UIView] in the centre of the scrollView so that 2 UIView appears on its left and 2 on its right.
    Though I have frame of 3rd UIView and when I pass it to

    Code:
    scrollRectToVisible
    
    method, that UIView becomes visible but not exactly in the centre.
    How can I make it to appear in the centre?

    Thanks
    Arnieterm
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    There is another instance method of UIScrollView that will help you do what you want to do. Hint: it's the one that follows scrollRectToVisible:animated: in the Class Reference.
     
  3. arnieterm thread starter macrumors regular

    Joined:
    Aug 28, 2008
    #3
    Thanks for the suggestion. I am already making use of that function. The problem is that the UIView that I needs to bring in the center position is already visible, so I think using the function
    Code:
    - (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
    
    does not work
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Setting the content offset doesn't work if the content is already visible?
     
  5. arnieterm thread starter macrumors regular

    Joined:
    Aug 28, 2008
    #5
  6. foobarpig macrumors newbie

    Joined:
    Jan 18, 2010
    #6
    Given code is for 50px width icons scrolled horizontally, so since your icons are the same width, you should be able to reuse that code without modifications. Anyway, if you have horizontal margins you should count all "area of interest" replacing 50 with "icon width + margins" value and 25 with a half of that value.

    That code checks if to which icon it should "clip" and then scrolls to its center point. And also prevents from scrolling too far.
     

Share This Page