我已经以编程方式创建了一个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];

推荐答案

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

Swift 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
}

Ios相关问答推荐

如何在进行滑动删除操作时保持自定义视图背景静态

如何仅在TextField中的文本发生更改时才执行代码?

在金属中设置纹理的UV坐标以编程方式变形纹理

当操作修改查看条件时,按钮不重复

使用标头UICollectionViewFlowLayout时的简单SwiftUI视图错误

在flatter_riverpod中,如果没有ref对象,我们如何访问提供程序?

许多文本字段的性能问题

为什么IOS多行文本输入在React原生纸张对话框中无法正常工作?

如何将 AppIntent 添加到应用程序目标并在交互式小部件中使用它

Task.cancel() 的 TaskPriority 的区别

SwiftUI 我自己的渲染函数的返回类型是什么?

如何在保持纵横比的同时zoom UIView 的内容以适应目标矩形?

UIView 和 initWithFrame 以及一个 NIB 文件.如何加载 NIB 文件?

为信用卡输入格式化 UITextField,例如 (xxxx xxxx xxxx xxxx)

针对 Xcode 中的 iPad 选项

Xcode:此时无法安装此应用程序.

以编程方式 Select UITextField 中的所有文本

水平滚动 UICollectionView 时对齐单元格的中心

iOS 7 半透明模态视图控制器

iOS 中的 Google Analytics SDK 3.0 _sqlite3 链接器错误