xcode inputAccessoryView在显示alertController(actionSheet)时向下动画显示

tuwxkamq  于 2023-03-04  发布在  其他
关注(0)|答案(4)|浏览(106)

我有一个聊天应用程序的inputAccessoryView,它总是保持可见,并停靠在屏幕底部用于文本输入,类似于大多数消息应用程序。
当我使用actionSheet样式显示alertController时,inputAccessoryView在显示警报时会向下移动到屏幕外,然后在解除警报时又返回到屏幕外,这反过来又会滚动tableView,这是不可取的。
发生这种情况的原因是,当出现警报时,聊天视图Controller正在放弃firstResponder。
有没有办法显示一个alertController而不给予firstResponder,或者当一个inputAccessoryView的视图放弃firstResponder时,保持它停靠在屏幕底部?

7kqas0il

7kqas0il1#

InputAccessoryView位于ViewController的层次结构之外--它包含在UITextEffectsWindow中,其rootViewController是UIInputWindowController。类似地,键盘包含在UIRemoteKeyboardWindow和它自己的UIInputWindowController中。
因此,如果我们从最上面的窗口或更高的窗口(UITextEffectsWindowUIRemoteKeyboardWindow)显示警报,它不会放弃第一响应者。
我找到的最简单的解决办法是:
x1米7英寸x1米8英寸
理想情况下,您可以安全地处理这些选项。一个可能更好的解决方案(我已经看到了前一个解决方案中的一些控制台错误)是创建一个具有更高WindowLevel的新UIWindow,使其成为关键窗口并可见,然后从那里显示警报。

b09cbbtk

b09cbbtk2#

感谢@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)
}
yzxexxkh

yzxexxkh3#

对于那些寻找Corey答案的Objective-C等价物的人:

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

atmip9wb4#

let rootViewController = UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.windowScene?.windows.last?.rootViewController
rootViewController?.present(createImagePickAlertController(vc: self, pickerController: pickerController), animated: false)

此问题可修复“键盘无法显示视图控制器(尝试显示〈UIAlertController:0x7fd63004d600〉)”错误。

相关问题