我有一個UITextField。點擊時軟鍵盤覆蓋UITextField。
如何將佈局向上移動,以便讓它在鍵盤上方顯示。
在此先感謝。如何在Xamarin.iOS中顯示軟件鍵盤時向上移動佈局
1
A
回答
0
參見樣品執行力度
變量用於容納所述文本框
private UIView activeview; // Controller that activated the keyboard
private float scroll_amount = 0.0f; // amount to scroll
private float bottom = 0.0f; // bottom point
private float offset = 10.0f; // extra offset
private bool moveViewUp = false; // which direction are we moving
鍵盤觀察員viewDidLoad中()中的控制器。
// Keyboard popup
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.DidShowNotification,KeyBoardUpNotification);
// Keyboard Down
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.WillHideNotification,KeyBoardDownNotification);
首先是KeyboardUpNotification方法。實際上,您可以計算控件是否被鍵盤隱藏,如果是,則計算視圖需要移動多少以顯示控件,然後移動它。
private void KeyBoardUpNotification(NSNotification notification)
{
// get the keyboard size
RectangleF r = UIKeyboard.BoundsFromNotification (notification);
// Find what opened the keyboard
foreach (UIView view in this.View.Subviews) {
if (view.IsFirstResponder)
activeview = view;
}
// Bottom of the controller = initial position + height + offset
bottom = (activeview.Frame.Y + activeview.Frame.Height + offset);
// Calculate how far we need to scroll
scroll_amount = (r.Height - (View.Frame.Size.Height - bottom)) ;
// Perform the scrolling
if (scroll_amount > 0) {
moveViewUp = true;
ScrollTheView (moveViewUp);
} else {
moveViewUp = false;
}
}
鍵盤按下事件很簡單。如果視圖已被移動,則將其顛倒。
private void KeyBoardDownNotification(NSNotification notification)
{
if(moveViewUp){ScrollTheView(false);}
}
滾動視圖將以動畫方式滾動視圖。
private void ScrollTheView(bool move)
{
// scroll the view up or down
UIView.BeginAnimations (string.Empty, System.IntPtr.Zero);
UIView.SetAnimationDuration (0.3);
RectangleF frame = View.Frame;
if (move) {
frame.Y -= scrollamount;
} else {
frame.Y += scrollamount;
scrollamount = 0;
}
View.Frame = frame;
UIView.CommitAnimations();
}
3
可以使用https://github.com/hackiftekhar/IQKeyboardManager
xamarin IOS結合https://github.com/TheEightBot/Xamarin.IQKeyboardManager
Xamarin的NuGet: https://www.nuget.org/packages/Xamarin.IQKeyboardManager/
+0
令人驚歎 - 嗨,他們很高興他們創造了這個 – cfl
相關問題
- 1. 顯示軟鍵盤時向上移動佈局?
- 2. 佈局屏幕向上移動時,鍵盤顯示
- 3. 移動軟鍵盤上方的佈局
- 4. 安卓移動佈局時,軟鍵盤顯示與查看器
- 5. Android如何確定在顯示軟鍵盤時是否要移動佈局?
- 6. 鍵盤顯示時向上移動scrollView
- 7. 如何在軟鍵盤開啓時移動線性佈局
- 8. 防止軟鍵盤向上移動佈局
- 9. 軟鍵盤向上移動所有佈局
- 10. 如何在軟鍵盤Android上顯示我的佈局?
- 11. 如何防止在鍵盤上顯示UITextView向上移動
- 12. 如何在軟鍵盤顯示時隱藏EditText軟鍵盤?
- 13. 如何在鍵盤上顯示我的滾動佈局?
- 14. 當鍵盤彈出時向上移動佈局
- 15. 在顯示軟鍵盤時更改佈局
- 16. 如何在鍵盤顯示時防止滑動佈局
- 17. 當軟鍵盤出現時移動佈局
- 18. 移動起來防止線性佈局時,軟鍵盤可見
- 19. 安卓工具欄在鍵盤顯示時向上移動
- 20. 打開EditText鍵盤時移動佈局
- 21. UIKeyboardWillShowNotification向上滑動鍵盤時顯示
- 22. 向上滾動軟鍵盤
- 23. 防止佈局的大小時,軟鍵盤顯示
- 24. Android的調整佈局的一部分時,軟鍵盤顯示
- 25. 顯示軟鍵盤時,Android佈局變得混亂
- 26. 當軟鍵盤打開時顯示佈局
- 27. 在輸入第一個字符時,軟鍵盤向上推動佈局
- 28. 顯示鍵盤時的佈局問題
- 29. 部分佈局disapear時鍵盤顯示
- 30. 在軟鍵盤上顯示'完成'鍵
嗨,我用您的解決方案,並遇到了一個問題。我的計算scroll_amount總是消極的...任何想法我做錯了什麼... Thx – Christoph