Huu*_*yen 4 xamarin.ios xamarin.android xamarin xamarin.forms
我想得到( )和( )Keyboard上的高度。AndroidiOSXamarin Forms
当Screen显示有Portraitmode和Landscapemode时,如何获取height值?
我找到了Swift关于的参考资料iOS:
不过可以给我源代码吗Xamarin
以及如何获得键盘的高度Android?
请帮我!
有两种方法可以做到这一点!
\n第一种方法:如果您的应用程序很小,则为一个 UIViewController 实现它,如下所示:
\n第 1 步:添加用于显示和隐藏观察者的字段:
\nprivate NSObject _keyboardObserverWillShow;\nprivate NSObject _keyboardObserverWillHide;\nRun Code Online (Sandbox Code Playgroud)\n步骤 2:更新 ViewDidLoad 和 ViewDidUnload 覆盖,以便在键盘显示或隐藏时添加/删除观察者:
\npublic override void ViewDidLoad() {\n base.ViewDidLoad ();\n _keyboardObserverWillShow = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidShowNotification, KeyboardDidShowNotification);\n _keyboardObserverWillHide = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, KeyboardWillHideNotification);\n}\n \npublic override void ViewDidUnload() {\n base.ViewDidUnload();\n NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillShow);\n NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillHide);\n}\nRun Code Online (Sandbox Code Playgroud)\n第 3 步:然后填充为每个观察者执行的函数KeyboardDidShowNotification& 。KeyboardWillHideNotification它相当长,需要我花一些时间来解释每个部分,但您可以在评论中询问我。其过程如下:
private void KeyboardWillHideNotification (NSNotification notification) \n{\n UIView activeView = View.FindFirstResponder();\n if (activeView == null)\n return;\n \n UIScrollView scrollView = activeView.FindSuperviewOfType (this.View, typeof(UIScrollView)) as UIScrollView;\n if (scrollView == null)\n return;\n \n // Reset the content inset of the scrollView and animate using the current keyboard animation duration\n double animationDuration = UIKeyboard.AnimationDurationFromNotification(notification);\n UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, 0.0f, 0.0f);\n UIView.Animate(animationDuration, delegate{\n scrollView.ContentInset = contentInsets;\n scrollView.ScrollIndicatorInsets = contentInsets;\n });\n} \n\nprivate void KeyboardDidShowNotification (NSNotification notification) \n{\n UIView activeView = View.FindFirstResponder();\n if (activeView == null)\n return;\n\n ((UITextField)activeView).ShowDoneButtonOnKeyboard();\n\n UIScrollView scrollView = activeView.FindSuperviewOfType(this.View, typeof(UIScrollView)) as UIScrollView;\n if (scrollView == null)\n return;\n \n RectangleF keyboardBounds = UIKeyboard.BoundsFromNotification(notification);\n \n UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, keyboardBounds.Size.Height, 0.0f);\n scrollView.ContentInset = contentInsets;\n scrollView.ScrollIndicatorInsets = contentInsets;\n \n // If activeField is hidden by keyboard, scroll it so it\'s visible\n RectangleF viewRectAboveKeyboard = new RectangleF(this.View.Frame.Location, new SizeF(this.View.Frame.Width, this.View.Frame.Size.Height - keyboardBounds.Size.Height));\n \n RectangleF activeFieldAbsoluteFrame = activeView.Superview.ConvertRectToView(activeView.Frame, this.View);\n // activeFieldAbsoluteFrame is relative to this.View so does not include any scrollView.ContentOffset\n \n // Check if the activeField will be partially or entirely covered by the keyboard\n if (!viewRectAboveKeyboard.Contains(activeFieldAbsoluteFrame)) {\n // Scroll to the activeField Y position + activeField.Height + current scrollView.ContentOffset.Y - the keyboard Height\n PointF scrollPoint = new PointF(0.0f, activeFieldAbsoluteFrame.Location.Y + activeFieldAbsoluteFrame.Height + scrollView.ContentOffset.Y - viewRectAboveKeyboard.Height);\n scrollView.SetContentOffset(scrollPoint, true);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n第二种方法:创建一个键盘处理程序,您可以通过BaseViewController将其用于每个页面
\n您可以按照此处 stackOverflow Xamarin 解决方案中提到的解决方案进行操作((Activity)Forms.Context).Window.SetSoftInputMode(SoftInput.AdjustPan);。有时,\xe2\x80\x99 不起作用,因此您可以使用它来修复它。PS:没有\xe2\x80\x99复制这些答案,因为\xe2\x80\x99没有重写答案的意义。
| 归档时间: |
|
| 查看次数: |
4797 次 |
| 最近记录: |