如何在虚拟键盘存在时显示UIToolBar

时间:2014-07-24 14:06:21

标签: ios objective-c uikeyboard

即使使用硬件键盘,也会显示工具栏。这是我用来添加工具栏的代码。我在viewWillAppear中运行此代码。

 UIToolbar* numberToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleDefault;
    numberToolbar.items = [NSArray  arrayWithObjects:
                           [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:NSLocalizedString(@"Close", @"Close") style:UIBarButtonItemStyleBordered target:self action:@selector(closeKeyboard)],
                           nil];
    [numberToolbar sizeToFit];
    self.mobileNumberTextField.inputAccessoryView = numberToolbar;

1 个答案:

答案 0 :(得分:0)

您可以使用NSNotificationCenter来识别键盘是显示还是隐藏。在viewDidLoad上添加以下行:

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

并实施方法来执行您的操作:

- (void)keyboardWillShow:(NSNotification*)notification
- (void)keyboardWillHide:(NSNotification*)notification

我希望它有所帮助。