2012-01-25 20 views
16

Tengo una barra de herramientas y me gustaría colocarla sobre el teclado.Agregar una barra de herramientas en la parte superior de la uikeyboard

En la notificación keyboardwillshow, He intentado añadir barra de herramientas para el teclado, pero sin suerte, no puedo añadir

por favor hágamelo saber

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

¿Qué intenta? –

+0

has probado algo? – Sarah

Respuesta

44

creo, desea una inputAccessoryView.

Básicamente, puede crear una vista y configurarla como una vista de entrada de accesorio de campo de texto o de vista de texto.

[textField setInputAccessoryView:inputAccessoryView]; 
+0

Este enlace es incorrecto (404) –

+0

Es curioso, hace unas horas que existía. – vikingosegundo

+0

Lo arreglé. Echar un vistazo. – vikingosegundo

14

Aquí es código en caso de que alguien necesidad it.Found el desbordamiento de pila

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

Este es el mejor controlador de teclado que he visto. Manera muy excelente de administrar entradas de texto.

Algunas de sus características 1) CERO línea de código

2) funciona automáticamente

3) No más de UIScrollView

4) No hay más subclases

5) trabajar más Manual

6) No más #importaciones

1

solución simple para iOS 8 y 9.

Init su textField, etc. searchBar

customView - Ver que se pegará en la parte superior del teclado. ¡NO lo agregue como una subvista a la jerarquía! No tiene que establecer ninguna restricción o posición.

[searchBar setInputAccessoryView:self.customView]; 

- (BOOL)canBecomeFirstResponder{ 
    return true; 
} 

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

} 

Si es necesario dejar que la CustomView en el fondo y no ocultarlo después de despedir teclado, añadir observador:

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

y el método

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

si quieres para agregar una barra de herramientas en el teclado, estás en el lugar correcto. Puedes usar BSKeyboard fácilmente. Mire la implementación a continuación;

en el archivo .h

#import "BSKeyboardControls.h" 

añadir delegados

@interface ViewController : UIViewController <BSKeyboardControlsDelegate> 

Ahora salta el archivo .m, van a viewDidLoad.Añadiremos campo de texto que quiero añadir herramientas tollbar

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

hay que añadir activeField al igual que a continuación,

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

y último paso es delegar metods implementación de dichas mejoras,

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

Eso es todo.

Para obtener más información y descargar los archivos BSKeyboard, puede mover el siguiente enlace BSKeyboard Githup Link

Cuestiones relacionadas