【问题标题】:Prevent hiding keyboard on presentViewController (showing popup)防止在 presentViewController 上隐藏键盘(显示弹出窗口)
【发布时间】:2014-08-24 19:26:58
【问题描述】:

上面有一个带有 UITextView 的视图。 UITextView 是键盘唯一可能的 firstResponder。我们称之为“BackView”。

还有另一个视图是 PopUp(具有透明背景的全屏视图,会淡化底层视图)。我们称之为“PopUp”。

PopUp 可以正常工作,除非键盘在屏幕上。呈现 PopUp 会强制隐藏键盘,当 PopUp 消失时,键盘会再次显示。由于 PopUp 没有覆盖整个 BackView,它看起来不太好。

有什么方法可以在显示 PopUp 时将键盘保持在 BackView 上? (弹出窗口不需要使用键盘)。

弹出窗口包含:

  • 全屏视图,背景设置为带有 alpha 的黑色(以淡化底层视图)
  • 多张图片、标签和关闭按钮。

PopUp 显示为:

[self setModalPresentationStyle:UIModalPresentationCurrentContext];
[self presentViewController:vc animated:YES completion:nil];

找到解决方案:

添加弹出视图作为当前窗口的子视图

【问题讨论】:

  • 如果我理解正确,您需要将“vc”添加为您的子视图控制器,然后将其视图添加为当前控制器(显示键盘的控制器)的子视图。不要要求系统呈现它,只需自己做(因为你没有改变上下文......至少你没有通过提供有关 PopUp 的详细信息来表示)。标记它,以便在您将其淡出后将其移除。
  • 感谢您的建议。尝试将 PopUp 作为子视图。在这种情况下,键盘显示在我的弹出窗口上方:(并且没有褪色
  • igraczech,再次感谢。如果将子视图添加到窗口(不是当前控制器),所有工作都很好。
  • 您也可以在弹出框内显示它。这也应该覆盖键盘。
  • igraczech,popover 实际上是我想要做的。但据我了解,它仅适用于 iPad?

标签: ios7 keyboard uitextview


【解决方案1】:

显示:

if let popupVC = storyboard.instantiateViewControllerWithIdentifier("PopupVC") as? PopupVC
    {
        var frame = UIScreen.mainScreen().bounds
        frame.origin.y = frame.size.height
        overlayWindow = UIWindow(frame: frame)

        popupVC.overlayWindow = overlayWindow
        overlayWindow.windowLevel = UIWindowLevelAlert
        overlayWindow.rootViewController = popupVC

        overlayWindow.makeKeyAndVisible()

        UIView.animateWithDuration(0.3, animations:
        {
            self.overlayWindow.frame.origin.y = 0
        })
    }

隐藏在 PopupVC 中

 if let window = overlayWindow
    {
        UIView.animateWithDuration(0.3, animations: {
            window.frame.origin.y = window.frame.height
        }, completion: { (finished) -> Void in
            self.overlayWindow = nil
        })
    }

【讨论】:

    【解决方案2】:

    mb.server 回答了他自己的问题(我也有)。但我只是想为了他人的利益明确说明代码:

    //instantiate popUpVC, then
    popUpVC.view.frame = [UIScreen mainScreen].bounds; //assuming you wish the popover to occupy the entire screen; adjust as needed
    UIWindow* currentWindow = [[[UIApplication sharedApplication] windows] lastObject];
    [currentWindow addSubview:popUpVC.view];
    

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 2020-06-12
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 2012-09-30
      • 1970-01-01
      相关资源
      最近更新 更多