2011-04-18 43 views
0

我需要爲我的應用程序定製一個tab切換視圖,所以我使用UIScrollView和UITabBar創建了一個.xib。在更改UIScrollView的內容時保持滾動行爲和滾動條可見性

我添加了一個「ClearChildren」和「SwitchViews」方法將UIScrollView的是這樣的:

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 
} 
然而

,當i開關上使用someUiScrollView.SwitchViews(someOtherScrollView) 一個UIScrollView的視圖的UIScrollView的突然失去其滾動條和允許水平和垂直滾動。

我該如何確保UIScrollView的水平和垂直滾動行爲不會改變,並保持滾動條可見?

回答

0

我正在加載的視圖竟然比UIScrollView稍寬。

如果這可以通過新的視圖的寬度設置爲UIScrollView中的像這樣的界限就能夠抑制:

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

(當然,這意味着該視圖只能有垂直滾動,這正是我想要的這個特殊的例子)

1

使用NSLog轉儲myScrollView.showsHoriontalScrollIndicator和myScrollView.showsVerticalScrollIndicator和myScrollView.scrollingEnabled的值以查看它們如何更改。

我也會小心刪除所有滾動視圖的子視圖。相反,當您添加子視圖時,請跟蹤它們(可能使用標籤或單獨的數組),並只刪除自己明確添加的子視圖。

最後,檢查切換前後contentSize和bounds(或frame)的相對值,因爲這可能也會影響滾動行爲。

+0

很好的建議,但我把問題本身扼殺在萌芽狀態,這確實是我加載視圖的界限。它們比scrollviewer稍寬一點,然後它自動啓用水平滾動。 感謝您的時間和+1 – 2011-04-18 14:25:40