2017-01-12 5 views
0

How do I achieve thisUISearchBar hinzufügen über Tastatur

ich eine Suchleiste über der Tastatur angezeigt werden soll. Immer wenn die Tastatur erscheint, sollte auch die Suchleiste erscheinen. Wie macht man das?

+0

kann u zeigen verwenden Ihr versuchte Code –

+0

Ich habe Suchleiste programmatisch erstellt und denke daran, es als Eingabeansicht der Tastatur hinzuzufügen ... Aber keine Idee, in Code zu implementieren ... –

Antwort

0

ein Objekt von UISearch bar Erstellen und einen Beobachter für Tastatur in textFieldDidBeginEditing/textViewDidBeginEditing hinzufügen

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification 
               object:nil]; 
} 

- (void)keyboardWasShown:(NSNotification *)notification 
{ 

CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

int height = MIN(keyboardSize.height,keyboardSize.width); 
int width = MAX(keyboardSize.height,keyboardSize.width); 

//set frame for searchbar 
    searchBar.frame = CGRectMake(0, self.view.frame.size.height - height, width, 50);  
    } 
+0

kein Glück ?? ..... – Developer

0
- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
} 

- (void)keyboardWillShow:(NSNotification *)notification 
{ 
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    [UIView animateWithDuration:0.3 animations:^{ 
     CGRect f = self.view.frame; 
     f.origin.y = -keyboardSize.height; 
     self.view.frame = f; 
    }]; 
} 

-(void)keyboardWillHide:(NSNotification *)notification 
{ 
    [UIView animateWithDuration:0.3 animations:^{ 
     CGRect f = self.view.frame; 
     f.origin.y = 0.0f; 
     self.view.frame = f; 
    }]; 
} 
3

Sie können einfach textField.inputAccessoryView = Ihre Suchleiste

textField.inputAccessoryView = mySearchBar; 
+0

Willkommen bei Stack Overflow! Während dieser Codeabschnitt die Frage beantworten kann, ist es besser, eine Beschreibung des Problems einzubeziehen, und wie Ihr Code das gegebene Problem angehen wird. Für die Zukunft gibt es hier einige Informationen zu Stack Overflow (http://stackoverflow.com/help/how-to-answer). – dirtydanee

Verwandte Themen