如何在键盘上方添加工具栏?


75

我已经以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];

1
您已经在这里有了答案。请检查...... stackoverflow.com/a/10594891/3615320
2014年


1
textField.inputAccessoryView = yourToolBar;
Vineesh TP

有一种比使用UIToolBar更新的方法,pinkstone.co.uk/…
neoneye

Answers:


132
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;

要关闭键盘:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

斯威夫特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:

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
}

2
或者可以使用类似此处的
Ramis

4
您可以通过添加此所有文本框UITextField.appearance().inputAccessoryView = /* your tool bar setup code */
查尔顿Provatas

在迅速4选择器中应如下所示:UIBarButtonItem(title:“ Done”,style:UIBarButtonItemStyle.done,target:self,action:#selector(self.OnTapDone(_ :)))
Mashhadi,

工具栏的宽度应根据屏幕大小来设置无法查看大小,编辑,在现在的方法一起关闭键盘
阿尔伯特·伦肖

100

您不再需要在代码中执行此操作。

  1. 只需将UIView拖动到当前场景的顶部栏,然后根据需要对其进行自定义即可。

在此处输入图片说明

  1. 在代码中简单地把IBOutlet两个:toolbarViewtextView并进行连接。

    @IBOutlet private var toolbarView: UIView!
    @IBOutlet private var textView: UITextView!
    
  2. viewDidLoad设置工具栏时,将其作为附件的附件UItextView

    override func viewDidLoad() {
        super.viewDidLoad()
    
        textView.inputAccessoryView = toolbarView
    }
    

结果如下:

在此处输入图片说明 在此处输入图片说明


11
哇。多年来,我一直在努力地做这件事;人们说故事板没有用!
Sirens

1
正如豪斯先生所说:人们撒谎;)
BartłomiejSemańczyk17年6

这难以置信!!效果很好。
AnBisw

1
不知道您可以这样做。太棒了!
Mike Simz '17

1
对于alertView输入文本,您可以使用以下代码:alert.textFields?.first?.inputAccessoryView =工具栏视图
oscar castellon

21

快速(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

9

您可以使用此代码为我工作。

-(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];
} 


4

迅捷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

1
textField.inputAccessoryView=[weakSelf addToolBar];
[textField setKeyboardType:UIKeyboardTypeNumberPad];

并添加一个方法

-(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;
}

第一部分是在键盘顶部添加工具栏。第二个创建的工具栏
DURGESH,2016年

谢谢您对ios的关注
DURGESH '16

@DURGESHKUMAR您能解释一下这行吗?textField.inputAccessoryView = [weakSelf addToolBar];
Gajendra K Chauhan '02

0

Swift 5.0及更高版本

           let toolBar = UIToolbar(frame: CGRect(x: 0.0,
                                                  y: 0.0,
                                                  width: UIScreen.main.bounds.size.width,
                                                  height: 44.0))//1
            let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)//2
            let DoneButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: #selector(tapDone))//3
            toolBar.setItems([flexibleSpace, DoneButton], animated: false)
           textField.inputAccessoryView = toolBar
       // Done Action
        @objc func tapDone() {
           self.view.endEditing(true)
         }

-1

在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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.