我有一个工具栏,我想把它放在键盘上。
在键盘将显示通知中,我试图将工具栏添加到键盘上,但没有成功,我无法添加
请让我知道
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。
基本上,您创建一个视图,并将其设置为文本字段或文本视图的输入附件视图。
[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
这是我见过的最好的键盘处理器。管理文本输入的非常好的方法。
它的一些特点1)代码的零行
2) 自动工作
3) 无更多UIScrollView
4) 不再有子类
5) 不再手动
6) 不再#导入
适用于iOS 8和9的简单解决方案。
初始化textField
、searchBar
等
customView
-将粘贴在键盘顶部的视图。不要将其作为子视图添加到层次结构中!不必设置任何约束或位置
[searchBar setInputAccessoryView:self.customView];
- (BOOL)canBecomeFirstResponder{
return true;
}
- (UIView *)inputAccessoryView {
return self.customView;
}
如果您需要让customView位于底部,并且在关闭键盘后不隐藏它,请添加observer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:self.view.window];
和方法
- (void)keyboardDidHide:(NSNotification *)notification {
[self becomeFirstResponder];
}
如果你想在键盘上添加工具栏,你就对了。你可以使用BSKeyboard。看看下面的实施情况;
in.h文件
#import "BSKeyboardControls.h"
添加代理
@interface ViewController : UIViewController <BSKeyboardControlsDelegate>
现在跳转.m文件,转到viewDidLoad。我们将添加文本字段,我想添加收费条
self.keyboardControls = [[BSKeyboardControls alloc] initWithFields:@[PINTextField]];
[self.keyboardControls addDelegate:self];
我们应该像下面一样添加activeField,
- (void)textFieldDidBeginEditing:(UIView *)textField {
[self.keyboardControls setActiveField:textField];
}
最后一步是代表方法的实现,
- (void)keyboardControlsDonePressed:(BSKeyboardControls *)keyboardControls {
[super textFieldShouldReturn:self.keyboardControls.activeField];
}
就是这样。
有关更多信息和下载BSKeyboard文件,您可以移动以下链接BSKeyboard Githup链接