How to scroll view up when keyboard appears?

后端 未结 7 1555
小蘑菇
小蘑菇 2021-01-31 02:54

I know that this question has been asked over and over again, but nothing seems to be working for me. Most of the solutions around are pretty out of date, and the rest are incre

7条回答
  •  感动是毒
    2021-01-31 03:23

    I spent sometime on this problem and gathered pieces code to create one final solution. My problem was related to UITableView scrolling and keyboard open/close.

    You need two partial methods in your cell class:

        void EditingBegin(UITextField sender)
        {
            // Height of tallest cell, you can ignore this!
            float tableMargin = 70.0f;
            float tableHeight = _tableView.Frame.Size.Height;
            float keyBoardHeight = KeyboardHeight();
    
            NSIndexPath[] paths = this._tableView.IndexPathsForVisibleRows;
            RectangleF rectLast = this._tableView.RectForSection(paths[paths.Length - 1].Section);
            RectangleF rectFirst = this._tableView.RectForSection(paths[0].Section);
            float lastCellY = rectLast.Y - rectFirst.Y;
            if (lastCellY > tableHeight - keyBoardHeight)
            {
                float diff = lastCellY - (tableHeight - tableMargin - keyBoardHeight);
                this._tableView.ContentInset = new UIEdgeInsets(0.0f, 0.0f, diff, 0.0f);
            }
    
            float cellPosition = this._tableView.RectForSection(this._section).Y;
            if (cellPosition > tableHeight - keyBoardHeight)
            {
                if (this._tableView.ContentInset.Bottom == 0.0f)
                {
                    float diff = cellPosition - (tableHeight - tableMargin - keyBoardHeight);
                    this._tableView.ContentInset = new UIEdgeInsets(0.0f, 0.0f, diff, 0.0f);
                }
                else
                {
                    this._tableView.ScrollToRow(NSIndexPath.FromItemSection(0, this._section), UITableViewScrollPosition.Middle, true);
                }
            }
        }
    
        partial void EditingEnd(UITextField sender)
        {
            UIView.BeginAnimations(null);
            UIView.SetAnimationDuration(0.3f);
            this._tableView.ContentInset = new UIEdgeInsets(0.0f, 0.0f, 0.0f, 0.0f);
            UIView.CommitAnimations();
        }
    

    and then in your view controller class:

        public override void WillAnimateRotation(UIInterfaceOrientation toInterfaceOrientation, double duration)
        {
            base.WillAnimateRotation(toInterfaceOrientation, duration);
    
            float bottom = this.TableView.ContentInset.Bottom;
            if (bottom > 0.0f)
            {
                if (toInterfaceOrientation == UIInterfaceOrientation.Portrait || toInterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown)
                {
                    bottom = bottom * UIScreen.MainScreen.Bounds.Width / UIScreen.MainScreen.Bounds.Height;
                }
                else
                {
                    bottom = bottom * UIScreen.MainScreen.Bounds.Height / UIScreen.MainScreen.Bounds.Width;
                }
    
                UIEdgeInsets insets = this.TableView.ContentInset;
                this.TableView.ContentInset = new UIEdgeInsets(0.0f, 0.0f, bottom, 0.0f);
            }
        }  
    

提交回复
热议问题