添加隐藏键盘的按钮

发布于 2024-11-10 09:18:13 字数 306 浏览 1 评论 0原文

在 UITextView 上隐藏键盘有一个方法:

...
    textfield.returnKeyType = UIReturnKeyDone;
    textfield.delegate = self;
....

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;

}

但是如果我想将“完成”按钮保留到“返回”并添加一个按钮来隐藏键盘,我该怎么办?

On a UITextView to hide the keyboard, there is the method:

...
    textfield.returnKeyType = UIReturnKeyDone;
    textfield.delegate = self;
....

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;

}

but if I want to leave the button "done" to the "return" and add a button to hide the keyboard, how do I?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

面如桃花 2024-11-17 09:18:13

您可以分配一个带有按钮的工具栏,该按钮可将键盘关闭为文本字段的 inputAccessoryView。一个简单的例子是,

UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)] autorelease];
UIToolbar *toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
toolbar.items = [NSArray arrayWithObject:barButton];

textField.inputAccessoryView = toolbar;

You can assign a toolbar with a button that dismisses the keyboard as the text field's inputAccessoryView. A quick example would be,

UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)] autorelease];
UIToolbar *toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
toolbar.items = [NSArray arrayWithObject:barButton];

textField.inputAccessoryView = toolbar;
自我难过 2024-11-17 09:18:13

Swift 2.0 版本:

//Declared at top of view controller
var accessoryDoneButton: UIBarButtonItem!
let accessoryToolBar = UIToolbar(frame: CGRectMake(0,0,UIScreen.mainScreen().bounds.width, 44))
//Could also be an IBOutlet, I just happened to have it like this
let codeInput = UITextField()

//Configured in viewDidLoad()
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(self.donePressed(_:)))
self.accessoryToolBar.items = [self.accessoryDoneButton]
self.codeInput.inputAccessoryView = self.accessoryToolBar

Swift 4:

//Declared at top of view controller
var accessoryDoneButton: UIBarButtonItem!
let accessoryToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
//Could also be an IBOutlet, I just happened to have it like this
let codeInput = UITextField()

//Configured in viewDidLoad()
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.donePressed))
self.accessoryToolBar.items = [self.accessoryDoneButton]
self.codeInput.inputAccessoryView = self.accessoryToolBar

func donePressed() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
}

UIToolBar 文档

'inputAccessoryView'文档

Swift 2.0 version:

//Declared at top of view controller
var accessoryDoneButton: UIBarButtonItem!
let accessoryToolBar = UIToolbar(frame: CGRectMake(0,0,UIScreen.mainScreen().bounds.width, 44))
//Could also be an IBOutlet, I just happened to have it like this
let codeInput = UITextField()

//Configured in viewDidLoad()
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(self.donePressed(_:)))
self.accessoryToolBar.items = [self.accessoryDoneButton]
self.codeInput.inputAccessoryView = self.accessoryToolBar

Swift 4:

//Declared at top of view controller
var accessoryDoneButton: UIBarButtonItem!
let accessoryToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
//Could also be an IBOutlet, I just happened to have it like this
let codeInput = UITextField()

//Configured in viewDidLoad()
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.donePressed))
self.accessoryToolBar.items = [self.accessoryDoneButton]
self.codeInput.inputAccessoryView = self.accessoryToolBar

func donePressed() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
}

UIToolBar Documentation

'inputAccessoryView' documentation

抚你发端 2024-11-17 09:18:13

这可以更容易地完成!

我在 IB 中创建了一个自定义视图,在 viewController.h 中,我刚刚创建了一个 IBOutlet UIView *accessoryView;,将它们连接起来并

添加 了一个 - (IBAction)dismissKeyboard;查看带有完成按钮的工具栏,与 IBAction 建立连接并写道:
[textView resignFirstResponder]

- (void)viewDidLoad
{
    textView.inputAccessoryView = accessoryView;
    [super viewDidLoad];
}

实际上这看起来有点奇怪并且非苹果风格......有什么想法吗?

This can be done ways easier!

I made a custom view in IB, in my viewController.h I just made an IBOutlet UIView *accessoryView;, connected them and an - (IBAction)dismissKeyboard;

I put in the view a toolbar with a done button, made a connection to the IBAction an wrote:
[textView resignFirstResponder] and

- (void)viewDidLoad
{
    textView.inputAccessoryView = accessoryView;
    [super viewDidLoad];
}

But actually that looks a bit strange and non-apple-style… Got an idea?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文