在uikeyboard上添加工具栏

我有一个工具栏,我想把它放在键盘上。

在keyboardwillshow通知,我试图添加到键盘的工具栏,但没有运气,我不能添加

请告诉我

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; UIView* keyboard; for(int i = 0; i < [tempWindow.subviews count]; i++) { //Get a reference of the current view keyboard = [tempWindow.subviews objectAtIndex:i]; //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) { [keyboard addSubview:myToolbar]; } } 

我想,你想要一个inputAccessoryView 。

基本上,您创build一个视图,并将其设置为文本字段或文本视图的input附件视图。

 [textField setInputAccessoryView:inputAccessoryView]; 

这里是代码,以防其他人需要它。发现堆栈溢出

 - (void)viewDidLoad { [super viewDidLoad]; UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; numberToolbar.barStyle = UIBarStyleBlackTranslucent; numberToolbar.items = [NSArray arrayWithObjects: [[UIBarButtonItem alloc]initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(clearNumberPad)], [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)], nil]; [numberToolbar sizeToFit]; numberTextField.inputAccessoryView = numberToolbar; } -(void)clearNumberPad{ [numberTextField resignFirstResponder]; numberTextField.text = @""; } -(void)doneWithNumberPad{ NSString *numberFromTheKeyboard = numberTextField.text; [numberTextField resignFirstResponder]; } 

https://github.com/asefnoor/IQKeyboardManager

这是我见过的最好的键盘处理程序。 非常好的pipe理文本input的方式。

它的一些特点1)ZERO的代码

2)自动工作

3)没有更多的UIScrollView

4)没有更多的子类

5)没有更多的手动工作

6)没有更多的#imports

简单的解决scheme为iOS 8和9。

初始化你的textFieldsearchBar

customView查看将坚持在键盘的顶部。 不要将它作为子视图添加到层​​次结构中! 不必设置任何约束或位置。

 [searchBar setInputAccessoryView:self.customView]; - (BOOL)canBecomeFirstResponder{ return true; } - (UIView *)inputAccessoryView { return self.customView; } 

如果你需要让customView在底部而不是隐藏键盘后隐藏它,添加观察者:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:self.view.window]; 

和方法

 - (void)keyboardDidHide:(NSNotification *)notification { [self becomeFirstResponder]; } 

如果你想在键盘上添加工具栏,你是正确的地方。 你可以使用BSKeyboard easly。 看下面的实现;

在.h文件中

 #import "BSKeyboardControls.h" 

添加代表

 @interface ViewController : UIViewController <BSKeyboardControlsDelegate> 

现在跳转.m文件,转到viewDidLoad。 我们将添加我想添加tollbar的textfield

 self.keyboardControls = [[BSKeyboardControls alloc] initWithFields:@[PINTextField]]; [self.keyboardControls addDelegate:self]; 

我们应该像下面一样添加activeField,

 - (void)textFieldDidBeginEditing:(UIView *)textField { [self.keyboardControls setActiveField:textField]; } 

最后一步是委托metods的实现,

  - (void)keyboardControlsDonePressed:(BSKeyboardControls *)keyboardControls { [super textFieldShouldReturn:self.keyboardControls.activeField]; } 

而已。

欲了解更多信息并下载BSKeyboard文件,您可以移动下面的链接BSKeyboard Githup链接