【问题标题】:Adding tool bar on top of the uikeyboard在 uikeyboard 顶部添加工具栏
【发布时间】:2012-02-18 08:48:11
【问题描述】:
我有一个工具栏,我想把它放在键盘上。
在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];
}
}
【问题讨论】:
标签:
objective-c
ios
cocoa-touch
uitoolbar
【解决方案1】:
我想,你想要一个inputAccessoryView。
基本上,您创建一个视图并将其设置为文本字段或文本视图的输入附件视图。
[textField setInputAccessoryView:inputAccessoryView];
【解决方案2】:
这是其他人需要的代码。在堆栈溢出时找到
- (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];
}
【解决方案4】:
适用于 iOS 8 和 9 的简单解决方案。
初始化你的textField、searchBar等
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];
}
【解决方案5】:
如果您想在键盘上添加工具栏,那么您来对地方了。您可以轻松使用 BSKeyboard。看下面的实现;
在.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 Link