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 ();
		}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值