ios 如何在键盘上方添加工具栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23904848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add a toolbar above the keyboard?
提问by Sushil Sharma
I have created a UIToolBar
programmatically and added a UITextField
on it. Now, I need that toolbar to be above the keyboard when I click in another text field.
我以UIToolBar
编程方式创建了一个并UITextField
在其上添加了一个。现在,当我单击另一个文本字段时,我需要该工具栏位于键盘上方。
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0,400, 320, 60)];
[self.view addSubview:toolBar];
UITextField *txtView=[[UITextField alloc]initWithFrame:CGRectMake(0, 400, 260, 30)];
txtView.backgroundColor =[UIColor grayColor];
txtView.placeholder=@"Address";
UIBarButtonItem *txtfieldItem=[[UIBarButtonItem alloc]initWithCustomView:txtView];
toolBar.items =[NSArray arrayWithObject:txtfieldItem];
回答by Kittu
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
phonenumberTextField.inputAccessoryView = numberToolbar;
To Dismiss Keyboard:
关闭键盘:
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
Swift 3:
斯威夫特 3:
let numberToolbar = UIToolbar(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 50))
numberToolbar.barStyle = UIBarStyle.Default
numberToolbar.items = [
UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelNumberPad"),
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneWithNumberPad")]
numberToolbar.sizeToFit()
phonenumberTextField.inputAccessoryView = numberToolbar
Swift 4.2:
斯威夫特 4.2:
let numberToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
numberToolbar.barStyle = .default
numberToolbar.items = [
UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelNumberPad)),
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneWithNumberPad))]
numberToolbar.sizeToFit()
phonenumberTextField.inputAccessoryView = numberToolbar
...
@objc func cancelNumberPad() {
//Cancel with number pad
}
@objc func doneWithNumberPad() {
//Done with number pad
}
回答by Bart?omiej Semańczyk
You do not need to do this in code anymore.
您不再需要在代码中执行此操作。
- Just simply drag UIView to the top bar of current scene and customize it as you want.
- 只需简单地将 UIView 拖到当前场景的顶部栏并根据需要对其进行自定义。
In code simply put
IBOutlet
for both:toolbarView
andtextView
and make connections.@IBOutlet private var toolbarView: UIView! @IBOutlet private var textView: UITextView!
In
viewDidLoad
setup your toolbarView as accessoryView of yourUItextView
.override func viewDidLoad() { super.viewDidLoad() textView.inputAccessoryView = toolbarView }
在代码中简单地
IBOutlet
同时输入:toolbarView
和textView
并建立连接。@IBOutlet private var toolbarView: UIView! @IBOutlet private var textView: UITextView!
在
viewDidLoad
将您的 toolbarView 设置为您的UItextView
.override func viewDidLoad() { super.viewDidLoad() textView.inputAccessoryView = toolbarView }
The result is following:
结果如下:
回答by Sunkas
For swift (1.2):
对于快速(1.2):
let numberToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
numberToolbar.barStyle = UIBarStyle.Default
numberToolbar.items = [
UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardCancelButtonTapped:"),
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardDoneButtonTapped:")]
numberToolbar.sizeToFit()
yourTextView.inputAccessoryView = numberToolbar
回答by Anil Prasad
You Can Use this code it work for me.
您可以使用此代码,它对我有用。
-(void)viewdidload
{
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered target:self
action:@selector(doneClicked:)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
textField.inputAccessoryView = keyboardDoneButtonView;
}
-(void)doneClicked:(id)sender
{
NSLog(@"Done Clicked.");
[self.view endEditing:YES];
}
回答by Anil Varghese
You can use the UITextField
s inputAccessoryViewproperty
您可以使用UITextField
s inputAccessoryView属性
txtField.inputAccessoryView = toolBar;
回答by fs_tigre
Swift 3
斯威夫特 3
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50))
toolBar.barStyle = UIBarStyle.default
toolBar.items = [
UIBarButtonItem(title: "Button1", style: UIBarButtonItemStyle.plain, target: self, action: #selector(test2)),
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Button2", style: UIBarButtonItemStyle.plain, target: self, action: #selector(test1))]
toolBar.sizeToFit()
myTextField.inputAccessoryView = toolBar
回答by DURGESH
textField.inputAccessoryView=[weakSelf addToolBar];
[textField setKeyboardType:UIKeyboardTypeNumberPad];
and add a method
并添加一个方法
-(UIToolbar *)addToolBar
{
UIBarButtonItem *done=[[UIBarButtonItem alloc]initWithTitle:@"DONE" style:UIBarButtonItemStyleDone target:self action:@selector(done:)];
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
NSArray* toolBarItems=[[NSArray alloc]initWithObjects:done, nil];
[toolBar setItems:toolBarItems];
return toolBar;
}
回答by Spydy
In Swift 3 and 4
在 Swift 3 和 4 中
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
toolBar.items = [
UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.sendCodeBtnAction(sender:)))]
tfPhone.inputAccessoryView = toolBar