当alertController(actionSheet)出现时,inputAccessoryView动画下来

时间:2017-10-29 02:17:32

标签: ios swift xcode uialertcontroller inputaccessoryview

我有一个用于聊天应用程序的inputAccessoryView,该应用程序始终保持可见并停靠在屏幕底部,用于文本输入,类似于大多数消息应用程序。

当我使用actionSheet样式呈现alertController时,inputAccessoryView会在显示警报时向下关闭屏幕,然后在警报解除时再次备份。这反过来滚动我的tableView,是不可取的。

这种情况正在发生,因为聊天viewController在发出警报时放弃了firstResponder。

无论如何都提出一个alertController并且不放弃firstResponder,或者无论如何在它的视图resignFirstResponder时将inputAccessoryView停靠在屏幕底部?

3 个答案:

答案 0 :(得分:6)

InputAccessoryView位于ViewController的层次结构之外 - 它包含在UITextEffectsWindow中,其rootViewController是UIInputWindowController。同样,键盘包含在UIRemoteKeyboardWindow及其自己的UIInputWindowController

因此,如果我们从最顶层窗口或更高窗口(UITextEffectsWindowUIRemoteKeyboardWindow)显示警报,则不会让第一响应者辞职。

我发现最简单的解决方案是:

let topViewController = UIApplication.shared.windows.last!.rootViewController! topViewController.present(alert, animated: true, completion: nil)

理想情况下,您可以安全地处理这些选项。一个可能更好的解决方案(我已经看到了之前解决方案中的一些控制台错误)是创建一个具有更高WindowLevel的新UIWindow,使其成为关键窗口并可见,并从那里显示警报。

答案 1 :(得分:1)

对于那些看起来像Corey答案的Objective-C的人:

UIViewController *objViewController = [UIApplication sharedApplication].windows.lastObject.rootViewController;
[objViewController presentViewController:view animated:YES completion:nil];

答案 2 :(得分:0)

感谢@Corey W.(对他投赞成票),我们对此问题有解决方案。 Swift 5中更安全的方法:

// Instantiate AlertController
let actionSheet = UIAlertController(title: title,
                                    message: "Comment options",
                                    preferredStyle: .actionSheet)

// Add actions
actionSheet.addAction(UIAlertAction(title: "Edit",
                                    style: .default,
                                    handler: { (_: UIAlertAction) in
                                        self.showEditingView(withCommentId: commentId, withCommentText: commentText)
}))

// Present AlertController
if let lastApplicationWindow = UIApplication.shared.windows.last,
    let rootViewController = lastApplicationWindow.rootViewController {
    rootViewController.present(actionSheet, animated: true, completion: nil)
}