Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. private UIView _activeview;
  2. private float _scrollAmount = 0.0f;    // amount to scroll
  3. private float _bottom = 0.0f;           // bottom point
  4. private float _offset = 10.0f;          // extra offset
  5. private bool _moveViewUp = false;
  6.  
  7. public override void ViewDidLoad()
  8. {
  9.     base.ViewDidLoad();
  10.  
  11.             // Keyboard popup
  12.             NSNotificationCenter.DefaultCenter.AddObserver
  13.             (UIKeyboard.DidShowNotification, KeyBoardUpNotification);
  14.  
  15.             // Keyboard Down
  16.             NSNotificationCenter.DefaultCenter.AddObserver
  17.             (UIKeyboard.WillHideNotification, KeyBoardDownNotification);
  18.  
  19.             UITapGestureRecognizer tapGestureRecognizer = new UITapGestureRecognizer(() =>
  20.             {
  21.                 //TODO: also should be good find all fields in View automatically
  22.                 NameSurnameField.ResignFirstResponder();
  23.                 UsernameField.ResignFirstResponder();
  24.                 CountryField.ResignFirstResponder();
  25.                 StatusField.ResignFirstResponder();
  26.                 EmailField.ResignFirstResponder();
  27.                 BirthdayField.ResignFirstResponder();
  28.                 GenderField.ResignFirstResponder();
  29.             });
  30.             View.AddGestureRecognizer(tapGestureRecognizer);
  31. }
  32.  
  33. private void KeyBoardUpNotification(NSNotification notification)
  34.         {
  35.             // get the keyboard size
  36.             CGRect r = UIKeyboard.BoundsFromNotification(notification);
  37.  
  38.             GetViewThatOpenKeyboard(View);
  39.  
  40.             if (_activeview == null)
  41.                 return;
  42.  
  43.             CGRect frameRelativeToParent = _activeview.ConvertRectToView(_activeview.Bounds, View);
  44.  
  45.             _bottom = (float)(frameRelativeToParent.Y + frameRelativeToParent.Height + _offset);
  46.  
  47.             // Calculate how far we need to scroll
  48.             _scrollAmount = (float)(r.Height - (View.Frame.Size.Height - _bottom));
  49.  
  50.             // Perform the scrolling
  51.             if (_scrollAmount > 0)
  52.             {
  53.                 _moveViewUp = true;
  54.                 ScrollTheView(_moveViewUp);
  55.             }
  56.             else
  57.             {
  58.                 _moveViewUp = false;
  59.             }
  60.         }
  61.  
  62.  private void GetViewThatOpenKeyboard(UIView view)
  63.         {
  64.             if (view.IsFirstResponder)
  65.             {
  66.                 _activeview = view;
  67.                 return;
  68.             }
  69.  
  70.             foreach (var v in view.Subviews)
  71.                 GetViewThatOpenKeyboard(v);
  72.         }
  73.  
  74. private void KeyBoardDownNotification(NSNotification notification)
  75.         {
  76.             if (_moveViewUp) { ScrollTheView(false); }
  77.         }
  78.  
  79. private void ScrollTheView(bool move)
  80.         {
  81.             // scroll the view up or down
  82.             UIView.BeginAnimations(string.Empty, IntPtr.Zero);
  83.             UIView.SetAnimationDuration(0.3);
  84.  
  85.             CGRect frame = View.Frame;
  86.  
  87.             if (move)
  88.             {
  89.                 frame.Y -= _scrollAmount;
  90.             }
  91.             else
  92.             {
  93.                 frame.Y += _scrollAmount;
  94.                 _scrollAmount = 0;
  95.             }
  96.  
  97.             View.Frame = frame;
  98.             UIView.CommitAnimations();
  99.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement