ios 在 uikeyboard 顶部添加工具栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8998134/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:28:49  来源:igfitidea点击:

Adding tool bar on top of the uikeyboard

objective-cioscocoa-touchuitoolbar

提问by user198725878

I have a toolbar and i would like to place it on top of the keyboard.

我有一个工具栏,我想把它放在键盘上。

In the keyboardwillshow notification, i tried to add toolbar to the keyboard but no luck, i cant add

在键盘将显示通知中,我尝试向键盘添加工具栏但没有运气,我无法添加

Please let me know

请告诉我

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

回答by vikingosegundo

I think, you want an inputAccessoryView.

我想,你想要一个inputAccessoryView

Basically, you create a view and set it as a text field's or text view's input accessory view.

基本上,您创建一个视图并将其设置为文本字段或文本视图的输入附件视图。

[textField setInputAccessoryView:inputAccessoryView];

回答by mhrrt

Here is code in case someone else need it.Found on stack overflow

这是代码以防其他人需要它。在堆栈溢出时找到

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

回答by i.AsifNoor

https://github.com/asefnoor/IQKeyboardManager

https://github.com/asefnoor/IQKeyboardManager

This is the best keyboard handler I have seen. Very excellent way to manage Text inputs.

这是我见过的最好的键盘处理程序。管理文本输入的非常好的方式。

Some of its features 1) ZERO LINE OF CODE

它的一些特点 1) 零代码行

2) Works Automatically

2) 自动工作

3) No More UIScrollView

3) 不再有 UIScrollView

4) No More Subclasses

4) 没有更多的子类

5) No More Manual Work

5) 没有更多的手工工作

6) No More #imports

6)没有更多#imports

回答by Jakub Truhlá?

Simple solution for iOS 8 and 9.

适用于 iOS 8 和 9 的简单解决方案。

Init your textField, searchBaretc.

INIT你textFieldsearchBar

customView- View that will stick at the top of the keyboard. DO not add it as a subview to hierarchy! Do not have to set any constraints or positions.

customView-将粘在键盘顶部的视图。不要将其作为子视图添加到层​​次结构中!不必设置任何约束或位置。

[searchBar setInputAccessoryView:self.customView];

- (BOOL)canBecomeFirstResponder{
    return true;
}

- (UIView *)inputAccessoryView {
    return self.customView;

}

If you need to let the customView at the bottom and not hide it after dismissing keyboard, add observer:

如果您需要让 customView 在底部而不是在关闭键盘后隐藏它,请添加观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:self.view.window];

And method

和方法

- (void)keyboardDidHide:(NSNotification *)notification {
    [self becomeFirstResponder];
}

回答by Emre Gürses

if you want to add tool bar on the keyboard, you are right place. You can use BSKeyboard easly. Look at the implementation below;

如果你想在键盘上添加工具栏,你就是对的地方。您可以轻松使用 BSKeyboard。看下面的实现;

in .h file

在 .h 文件中

#import "BSKeyboardControls.h"

add delegates

添加代表

@interface ViewController : UIViewController <BSKeyboardControlsDelegate>

now jump the .m file , go to viewDidLoad. We will add textfield which i want to add tollbar

现在跳转 .m 文件,转到 viewDidLoad。我们将添加我想添加收费栏的文本字段

self.keyboardControls = [[BSKeyboardControls alloc] initWithFields:@[PINTextField]];
[self.keyboardControls addDelegate:self];

we should add activeField just like below,

我们应该像下面一样添加activeField,

- (void)textFieldDidBeginEditing:(UIView *)textField {
    [self.keyboardControls setActiveField:textField];
}

And last step is delegate metods implemantation,

最后一步是委托方法实现,

 - (void)keyboardControlsDonePressed:(BSKeyboardControls *)keyboardControls {
    [super textFieldShouldReturn:self.keyboardControls.activeField];
}

That's it.

就是这样。

For more info and download the BSKeyboard files, you can move the below link BSKeyboard Githup Link

有关更多信息和下载 BSKeyboard 文件,您可以移动以下链接BSKeyboard Githup 链接