UIToolbar 未添加到键盘
Posted
技术标签:
【中文标题】UIToolbar 未添加到键盘【英文标题】:UIToolbar not adding to Keyboard 【发布时间】:2011-06-11 17:48:54 【问题描述】:我正在尝试将 UIToolbar 添加到 UIKeyboard,以便用户名可以在两个文本字段之间轻松切换。但是我无法让它工作。请你能告诉我我做错了什么。我已经在 viewDidLoad 中设置了 NotificationCenter。
- (void)keyboardWillShow:(NSNotification *)notification
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
// Now iterating over each subview of the available windows
for (UIView *keyboard in [keyboardWindow subviews])
// Check to see if the description of the view we have referenced is UIKeyboard.
// If so then we found the keyboard view that we were looking for.
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
NSValue *v = [[notification userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
CGRect kbBounds = [v CGRectValue];
UIToolbar *input = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kbBounds.size.width, 50)];
UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStylePlain target:self action:@selector(previousTextField:)];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextTextField:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideKeyboard:)];
[input setItems:[NSArray arrayWithObjects:previous,next,space,done,nil]];
if(input == nil)
UIToolbar *input = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kbBounds.size.width, 50)];
UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStylePlain target:self action:@selector(previousTextField:)];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextTextField:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideKeyboard:)];
[input setItems:[NSArray arrayWithObjects:previous,next,space,done,nil]];
[input removeFromSuperview];
input.frame = CGRectMake(0, 0, kbBounds.size.width, 30);
[keyboard addSubview:input];
keyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height + 60);
for(UIView* subKeyboard in [keyboard subviews])
if([[subKeyboard description] hasPrefix:@"<UIKeyboardImpl"] == YES)
subKeyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y - 30, kbBounds.size.width, kbBounds.size.height);
【问题讨论】:
【参考方案1】:创建UIToolbar
的实例,在工具栏中添加您需要的任何视图,然后在UITextField
的inputAccessoryView
属性中分配您的工具栏实例。
myTextFieldorTextView.inputAccessoryView = boolbar;
查看以下分步指南,将工具栏放在键盘顶部。
Adding a Toolbar with Next & Previous above UITextField Keyboard
【讨论】:
以上是关于UIToolbar 未添加到键盘的主要内容,如果未能解决你的问题,请参考以下文章
为多个 UITextField 使用 inputAccessoryView UIToolbar
UIBarButtonItems 未显示在 UIToolbar 上
未检测到 UIToolBar 上 UIToolBar 上的模态视图中的 UIButton 触摸未检测到