ios 键盘覆盖UI问题

在通知中心添加如下俩个通知,

//键盘显示

NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.DidShowNotification, KeyBoardUpNotifiaction);

//键盘隐藏

NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.DidHideNotification, keyBoardDownNotification);

UIView activeView;
		float scroll_amount=0.0f;
		float bottom=0.0f;
		float offset=20.0f;
		bool moveViewUp=false;
		/// <summary>
		/// 获取相应控件的view
		/// </summary>
		/// <param name="main_view">Main view.</param>
		void GetActiveView(UIView main_view)
		{
			foreach (var view in main_view.Subviews) {
				if (<span style="font-family: Arial, Helvetica, sans-serif;">view</span>.IsFirstResponder) {
					activeView = view;
				}
				
			}

		}

		void KeyBoardUpNotifiaction(NSNotification notification)
		{
			RectangleF r = UIKeyboard.BoundsFromNotification (notification);
			GetActiveView (this.View);
			if (activeView==null) {
				return;
			}
			bottom = (activeView.Frame.Y+activeView.Frame.Height+offset);

			if (scroll_amount>0) {
				return;
			}

			scroll_amount = (r.Height-(View.Frame.Size.Height-bottom));

			if (scroll_amount > 0) {
				moveViewUp = true;
				scrollTheView( moveViewUp);
			} else {
				moveViewUp = false;
			}
		}

		void keyBoardDownNotification(NSNotification notifiaction)
		{
			if (moveViewUp||scroll_amount>0f) {
				scrollTheView (false);
			}
		}

		void scrollTheView(bool move)
		{
			UIView.BeginAnimations (string.Empty, System.IntPtr.Zero);
			UIView.SetAnimationDuration (0.3);
			RectangleF frame = this.ui_view.Frame;
			if (move) {
				frame.Y -= (scroll_amount);
			} else {
				frame.Y += (scroll_amount);
				scroll_amount = 0;
			}
			this.ui_view.Frame = frame;
			UIView.CommitAnimations ();
		}




posted @ 2014-10-09 14:50  mendel  阅读(187)  评论(0编辑  收藏  举报