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

时间:2019-06-28 07:29:49

标签: c# ios xamarin.ios interface-builder uinavigationbar

说明:我得到一个简单的UIViewController,其中仅包含一个大的UITextView。有4个简单的约束:顶部,右侧,底部和左侧。没什么。

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

演示

  • UIViewController's背景为蓝色
  • UITextView's背景为红色
  • 请注意,当keyboard第一次出现 时,一切似乎都很好,但是随后出现了UINavigationBar问题...

编辑:

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

demo

2 个答案:

答案 0 :(得分:0)

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

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

   IQKeyboardManager.SharedManager.Enable = true;

   return true;
}

更新

我在不使用stroryboard的情况下创建了一个演示,您可以参考它。

在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;
}
  

在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

答案 1 :(得分:0)

似乎我的问题是由于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;
}
相关问题