开发者

maintain scrolling-behavior and scrollbar visibility when changing contents of UIScrollView

I required a custom tab-switching view for my application, so i made a .xib with a UIScrollView and a UITabBar.

I added a 'ClearChildren' and a 'SwitchViews' method to the UIScrollView like this :

public static class Extensions
{
    #region UIScrollView extensions
    public static void ClearChildren (this UIScrollView scrollViewer)
    {
        foreach(var subview in scrollViewer.Subviews)
        {
            subview.RemoveFromSuperview();
        }
    }

    /// <summary>
    /// switches the content views of a UIScrollView entirely
    /// with for example a tab-bar.
    /// </summary>
    /// <param name="scrollViewer">
    /// A <see cref="UIScrollView"/>
    /// </param>
    /// <param name=开发者_开发问答"newView">
    /// A <see cref="UIView"/>
    /// </param>
    public static void SwitchViews (this UIScrollView scrollViewer, UIView newView)
    {

        scrollViewer.ClearChildren();
        scrollViewer.AddSubview(newView);



        //reset content size
        scrollViewer.ContentSize = new System.Drawing.SizeF(newView.Bounds.Width, newView.Bounds.Height);

    }


    #endregion
}

however, when i switch the views on a UIScrollView using someUiScrollView.SwitchViews(someOtherScrollView) The UIScrollView suddenly loses its scroll bars and allows both horizontal and vertical scrolling.

how do i make sure the horizontal and vertical scrolling behavior of the UIScrollView do not change, and keep my scrollbars visible?


Use NSLog to dump the values of myScrollView.showsHoriontalScrollIndicator and myScrollView.showsVerticalScrollIndicator and myScrollView.scrollingEnabled to see how they change.

I'd also be careful about removing all subviews of your scroll view. Instead, as you add subviews, keep track of them (maybe with a tag, or in a separate array), and only remove those subviews that you've explicitly added yourself.

Finally, check the relative values of contentSize and bounds (or frame) before and after the switch, since this might affect scrolling behavior too.


The view i was loading turned out to be a little bit wider than the UIScrollView.

If this could be prevented simply by setting the new view's width to the bounds of the UIScrollView like so :

...
            scrollViewer.ClearChildren();
            newView.Bounds = new System.Drawing.RectangleF(new PointF(0f,0f), new SizeF(scrollViewer.Bounds.Width, newView.Bounds.Height));
            scrollViewer.AddSubview(newView);
...

(naturally this means the view can have vertical scrolling only, which is what i wanted in this particular example)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜