2012-01-25 78 views
16

我有一個工具欄,我想將它放在鍵盤上。在uikeyboard上添加工具欄

在keyboardwillshow通知,我試圖工具欄添加到鍵盤,但沒有運氣,我不能添加

請讓我知道

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

那你試試? –

+0

你有試過什麼嗎? – Sarah

回答

44

我認爲,你想要一個inputAccessoryView

基本上,您創建一個視圖並將其設置爲文本框或文本視圖的輸入附件視圖。

[textField setInputAccessoryView:inputAccessoryView]; 
+0

這個鏈接是不正確的(404) –

+0

有趣的是,幾個小時前它已經存在。 – vikingosegundo

+0

我修好了。看一看。 – vikingosegundo

14

這裏是萬一別人需要的代碼it.Found在堆棧溢出

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

https://github.com/asefnoor/IQKeyboardManager

這是最好的鍵盤處理程序我都看到了。非常好的管理文本輸入的方式。

它的一些特性1)零線CODE OF

2)自動工作

3)沒有更多的UIScrollView

4)沒有更多的子類

5)沒有更多的手動工作

6)No More #imports

1

爲iOS 8簡單的解決方案和9

初始化你textFieldsearchBar

customView - 認爲將繼續堅持在鍵盤的頂部。不要將它作爲子視圖添加到層​​次結構中!不必設置任何約束或位置。

[searchBar setInputAccessoryView:self.customView]; 

- (BOOL)canBecomeFirstResponder{ 
    return true; 
} 

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

} 

如果你需要讓customView底部,解聘鍵盤後不能掩蓋它,添加觀察:

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

和方法

- (void)keyboardDidHide:(NSNotification *)notification { 
    [self becomeFirstResponder]; 
} 
1
如果你想

在鍵盤上添加工具欄,你是正確的地方。你可以使用BSKeyboard easly。看看下面的實現;

在.h文件中

#import "BSKeyboardControls.h" 

添加代理

@interface ViewController : UIViewController <BSKeyboardControlsDelegate> 

現在跳起來.m文件,請viewDidLoad中。我們將添加文本框,我想添加tollbar

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

我們應該加上activeField就像下面,

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

而最後一步是委託metods implemantation,

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

就是這樣。

欲瞭解更多信息,並下載BSKeyboard文件,您可以將以下鏈接BSKeyboard Githup Link

相關問題