通过键盘显示UIAlertController [英] Show UIAlertController over keyboard
问题描述
在iOS 8及更低版本中,在显示键盘时显示UIActionSheet
,将在键盘上显示操作表.在iOS 9中,情况已不再如此.
In iOS 8 and lower show a UIActionSheet
when keyboard is presented will present the action sheet over the keyboard. With iOS 9 this is no longer the case.
在我的应用中,我们具有聊天功能,希望通过键盘显示操作.我们曾经使用UIActionSheet
在iOS 8之前都可以正常工作.在iOS 9中,操作表位于键盘后面.我已经尝试了UIActionSheet
和UIAlertController
.
In my app we have a chat functionality and want the show a action over the keyboard. We used to use UIActionSheet
which worked fine until iOS 8. In iOS 9 the action sheet is present behind the keyboard. I've tried both UIActionSheet
and UIAlertController
.
我们想要的是一个类似于messages.app中的操作表
What we want is a action sheet like in messages.app
我尝试将操作表放在其自己的窗口中,并覆盖canBecomeFirstResponder
,这只是使键盘消失了.
I've tried placing the action sheet in it own window and overriding canBecomeFirstResponder
which just made the keyboard disappear.
推荐答案
我已经在我们的应用程序中实现了此功能.诀窍是使警报控制器出现在不同的窗口中. UIActionSheet
实现就是这样做的,并且在iOS 8上运行良好,但是在9上,Apple已将键盘实现移动到窗口级别很高(10000000)的窗口.解决方法是为您的警报窗口提供更高的窗口级别(作为自定义double值,而不使用提供的常量).
I have implemented exactly this in our app. The trick is to have the alert controller appear on a different window. This is how the UIActionSheet
implementation does it, and works great on iOS 8, but on 9, Apple has moved the keyboard implementation to a window which has a very high window level (10000000). The fix is to give your alert window an even higher window level (as a custom double value, not using the provided constants).
在使用具有透明性的自定义窗口时,请确保在背景颜色方面阅读我在这里的答案防止窗口在旋转过渡期间变黑.
When using a custom window which will have transparency, make sure to read my answer here, regarding background color, to prevent window becoming black during rotation transitions.
_alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_alertWindow.rootViewController = [UIViewController new];
_alertWindow.windowLevel = 10000001;
_alertWindow.hidden = NO;
_alertWindow.tintColor = [[UIWindow valueForKey:@"keyWindow"] tintColor];
__weak __typeof(self) weakSelf = self;
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Test" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
weakSelf.alertWindow.hidden = YES;
weakSelf.alertWindow = nil;
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Test" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
weakSelf.alertWindow.hidden = YES;
weakSelf.alertWindow = nil;
}]];
[_alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
这篇关于通过键盘显示UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!