2017-02-03 78 views
2

相當惱人的問題,但它似乎好像在滾動視圖上有一些默認行爲,當您更改方向時,它會自動滾動到頂部。 ShouldScrollToTopUIScrollView上的一個屬性,但似乎無法在XF ScrollView中使用。但它無論如何呢?有沒有辦法阻止?防止自動滾動在XF的方向更改頂部滾動查看

回答

3

我只是做了一個快速測試,只發現它是iOS上的問題。在Android上,ScrollView在旋轉後不會滾動到頂部。我沒有測試UWP/WinPhone。如果只是iOS,應該針對Xamarin.Forms提交一個錯誤。

與此同時,這裏是iOS的解決方法。您將需要爲您的ScrollView創建一個自定義渲染器[1]。和渲染代碼應該是這樣的:

public class MyScrollViewRenderer : ScrollViewRenderer 
{ 
    CGPoint offset; 

    protected override void OnElementChanged(VisualElementChangedEventArgs e) 
    { 
     base.OnElementChanged(e); 

     UIScrollView sv = NativeView as UIScrollView; 
     sv.Scrolled += (sender, evt) => { 
      // Checking if sv.ContentOffset is not 0,0 
      // because the ScrollView resets the ContentOffset to 0,0 when rotation starts 
      // even if the ScrollView had been scrolled (I believe this is likely the cause for the bug). 
      // so you only want to set offset variable if the ScrollView is scrolled away from 0,0 
      // and I do not want to reset offset to 0,0 when the rotation starts, as it would overwrite my saved offset. 
      if (sv.ContentOffset.X != 0 || sv.ContentOffset.Y != 0) 
       offset = sv.ContentOffset; 

     }; 
     // Subscribe to the oreintation changed event. 
     NSNotificationCenter.DefaultCenter.AddObserver(this, new Selector("handleRotation"), new NSString("UIDeviceOrientationDidChangeNotification"), null); 
    } 

    [Export("handleRotation")] 
    void HandleRotation() 
    { 
     if (offset.X != 0 || offset.Y != 0) { 
      UIScrollView sv = NativeView as UIScrollView; 
      // Reset the ScrollView offset from the last saved offset. 
      sv.ContentOffset = offset; 
     } 
    } 
} 

[1] https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/

+0

我知道自定義渲染器是什麼,但這看起來很棒。我一定會在明天嘗試一下並檢查它是否有效。謝謝! – jdmdevdotnet

+0

提交的錯誤報告:https://bugzilla.xamarin.com/show_bug.cgi?id = 52510 – jgoldberger

3

我已延長@ jgoldberger的答案,並拿出了這一點:

[assembly: ExportRenderer(typeof(Xamarin.Forms.ScrollView), typeof(MyApp.iOS.CustomRenderers.Controls.FieldStrikeScrollViewRenderer))] 
namespace MyApp.iOS.CustomRenderers.Controls 
{ 
    public class MyScrollViewRenderer : ScrollViewRenderer 
    { 
     CGPoint offset; 

     protected override void OnElementChanged(VisualElementChangedEventArgs e) 
     { 
      base.OnElementChanged(e); 

      UIScrollView sv = NativeView as UIScrollView; 
      sv.Scrolled += (sender, evt) => { 
       // Checking if sv.ContentOffset is not 0,0 
       // because the ScrollView resets the ContentOffset to 0,0 when rotation starts 
       // even if the ScrollView had been scrolled (I believe this is likely the cause for the bug). 
       // so you only want to set offset variable if the ScrollView is scrolled away from 0,0 
       // and I do not want to reset offset to 0,0 when the rotation starts, as it would overwrite my saved offset. 
       if (sv.ContentOffset.X != 0 || sv.ContentOffset.Y != 0) 
        offset = sv.ContentOffset; 

      }; 
     } 

     public override void LayoutSubviews() 
     { 
      if (offset.X != 0 || offset.Y != 0) 
      { 
       UIScrollView sv = NativeView as UIScrollView; 
       // Reset the ScrollView offset from the last saved offset. 
       sv.ContentOffset = offset; 
      } 
      base.LayoutSubviews(); 
     } 
    } 
} 

,但他是正確的,這應該真的由Xamarin修復