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?
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 :
(naturally this means the view can have vertical scrolling only, which is what i wanted in this particular example)