相當惱人的問題,但它似乎好像在滾動視圖上有一些默認行爲,當您更改方向時,它會自動滾動到頂部。 ShouldScrollToTop
是UIScrollView
上的一個屬性,但似乎無法在XF ScrollView中使用。但它無論如何呢?有沒有辦法阻止?防止自動滾動在XF的方向更改頂部滾動查看
2
A
回答
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/
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修復
相關問題
- 1. 如何防止textarea在值更改時滾動到頂部?
- 2. 防止contenteditable自動滾動
- 3. jquery平滑滾動 - 防止滾動到頂部
- 4. 防止ListView滾動到頂部on notifyDAtaSetChanged
- 5. 防止CSS模式滾動到頂部
- 6. fullpage.js在滾動方向上更改滾動方向
- 7. Android:將TabLayout引腳滾動到頂部滾動查看
- 8. 滾動查看不滾動到線性佈局頂部
- 9. 滾動查看不滾動到底部
- 10. 更改滾動方向
- 11. 在定向更改期間,UIScrollView在SetContentSize之後自動滾動到頂部 - 我該如何防止該問題?
- 12. 動畫div的頂部向下滾動
- 13. 自動滾動到頂部在UITableView
- 14. 滾動查看不滾動
- 15. 滾動查看不滾動
- 16. 當數據源更改時,如何防止DataGridView自動滾動?
- 17. 用戶滾動更改邊距頂部
- 18. 滾動查看中的TabHost滾動內容更改
- 19. 將滾動位置存儲在滾動方向更改
- 20. 如何防止自動滾動DataGrid中
- 21. UIScrollView在橫向更改期間自動滾動到底部
- 22. 防止列表框滾動到頂部時更新
- 23. Listview自動滾動到頂部
- 24. RecyclerView自動滾動到頂部
- 25. CSS防止滾動
- 26. 防止iframe滾動
- 27. Fullpage.js - 防止滾動
- 28. 防止UITableView滾動?
- 29. 滾動查看不滾動子視圖在自動佈局
- 30. 滾動100vh之後的滾動滾動頂部
我知道自定義渲染器是什麼,但這看起來很棒。我一定會在明天嘗試一下並檢查它是否有效。謝謝! – jdmdevdotnet
提交的錯誤報告:https://bugzilla.xamarin.com/show_bug.cgi?id = 52510 – jgoldberger