UIKeyboardWillShowNotification,如何区分外部和iOS键盘

时间:2016-05-14 16:18:31

标签: ios objective-c iphone keyboard

在iOS模拟器中,我注意到尽管没有实际的键盘弹出,但UIKeyboardWillShowNotification事件仍然存在。我可以使用命令按钮+ K键切换键盘,但是我遇到了所有这些问题。

以下是设置事件调度程序的代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];

我的问题是,当这个事件进来时,iOS键盘实际上并没有弹出,这对我来说是一个问题,因为我有以下UIBarButtonItem,当我在PC上使用外接键盘时,STILL会显示它,我怀疑是默认键盘的默认类型。这是条形码的代码:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Tap Here to Dismiss the Keyboard" style:UIBarButtonItemStylePlain target:self action:@selector(keyboardDoneButtonClicked:)];
[doneButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14.0], NSFontAttributeName, [UIColor blackColor], NSForegroundColorAttributeName,[UIColor blackColor],NSBackgroundColorAttributeName, nil] forState:UIControlStateNormal];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, flexibleSpace, nil]];
[keyboardDoneButtonView setHidden:true];
self.labelValueTextView.inputAccessoryView = keyboardDoneButtonView;

那就是说,当UIKeyboardWillShowNotification出现时,如何区分外部键盘和iOS键盘?

1 个答案:

答案 0 :(得分:0)

这似乎可以在不同的iOS设备之间可靠地运行:

self.keyboardToolbar = [[UIToolbar alloc] init];
[self.keyboardToolbar sizeToFit];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Tap Here to Dismiss the Keyboard" style:UIBarButtonItemStylePlain target:self action:@selector(toolbarDoneButtonClicked:)];
[doneButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14.0], NSFontAttributeName, [UIColor blackColor], NSForegroundColorAttributeName,[UIColor blackColor],NSBackgroundColorAttributeName, nil] forState:UIControlStateNormal];
self.labelValueTextView.inputAccessoryView = self.keyboardToolbar;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHideNotification:) name:UIKeyboardDidHideNotification object:nil];

- (void)keyboardWillShowNotification:(NSNotification *)notification {
    CGRect frame = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:self.view.window];
    if ((frame.origin.y + frame.size.height) > self.view.window.frame.size.height)
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [self.labelValueTextView.inputAccessoryView setHidden:true];
        });
    else
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [self.labelValueTextView.inputAccessoryView setHidden:false];
        });
}

- (void)keyboardDidHideNotification:(NSNotification *)notification {
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        [self.labelValueTextView.inputAccessoryView setHidden:false];
    });
}

有一种情况可能发生在模拟器中,据我所知在实践中不会发生,这使用户能够在特定情况下在软键盘和非软键盘之间切换,即:能够在切换后切换软键盘的情况。我认为当软件键盘打开时插入外接键盘时,它会进入混合状态,直到用户通过上面代码中的工具栏按钮解开软件键盘,我想(但是不能测试,就像我做的那样)没有任何外部iOS兼容软件键盘)。如果有人能够对此有所了解,那将非常感激。