UINavigationBar 在键盘打开时被推出屏幕(UITextView)

The UINavigationBar is pushed out of the screen when the keyboard opens (UITextView)

描述 : 我得到了一个简单的 UIViewController,它只包含一个大的 UITextView。有 4 个简单的约束:顶部、右侧、底部和左侧。没什么好看的。

我的问题是什么:当用户开始在 UITextView 中键入文本时,keyboard 打开并且 UINavigationBar 得到出屏幕。我希望 UINavigationBar 留在同一个地方。

演示 :

编辑:

这是一个演示项目:https://drive.google.com/file/d/1lMtXNWLyEXDQMy7eeyu-sQrmsTja-tvO/view?usp=sharing

您可以在 iOS 项目上从 Nuget 安装 Xamarin.IQKeyboardManager。然后在AppDelegate中开启:

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
   //... 

   IQKeyboardManager.SharedManager.Enable = true;

   return true;
}

更新:

我在没有使用stroryboard的情况下创建了一个demo,你可以参考。

在 AppDelegate 中

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
   // create a new window instance based on the screen size
   Window = new UIWindow(UIScreen.MainScreen.Bounds);
   Window.RootViewController = new UINavigationController(new MainViewController());
   IQKeyboardManager.SharedManager.Enable = true;

   // make the window visible
   Window.MakeKeyAndVisible();

   return true;
}

in MainViewController

public override void ViewDidLoad()
{
  View = new UniversalView();

  base.ViewDidLoad();

  Title = "Title";

  IQTextView textView = new IQTextView() {

      Frame = new CGRect(20, 90, UIScreen.MainScreen.Bounds.Width - 40, 626),
      BackgroundColor = UIColor.Red,
      Text = "xxx",
      Font = UIFont.SystemFontOfSize(14),
  };

  View.AddSubview(textView);

  // Perform any additional setup after loading the view
}

您的问题可能是因为您使用了自动布局。您可以使用 Nuget 中的 Masonry

我的问题似乎是因为 IQKeyboardManager

我通过以下方式解决了我的问题:

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);
    Xamarin.IQKeyboardManager.SharedManager.Enable = false;
}

public override void ViewWillDisappear(bool animated)
{
    base.ViewWillDisappear(animated);
    Xamarin.IQKeyboardManager.SharedManager.Enable = true;
}