2014-01-14 2 views
0

В моем приложении для iPhone у меня есть UIWebView с панелями инструментов над ним ниже. панель инструментов внизу содержит текстовое поле для ввода пользователем некоторого текста. но когда я нажимаю на текстовое поле, клавиатура закрывает нижнюю половину экрана.Усадка UIWebView при появлении клавиатуры

Как сделать так, чтобы панели инструментов находились выше и ниже веб-просмотра, но высота веб-просмотра сжимается для отображения клавиатуры?

любое руководство по этому вопросу оценивается.

спасибо в продвинутом виде.

ответ

0

Вы можете реализовать UITextFieldDelegate в ваш UIViewController и установите значение делегирования UITextField на ваш контроллер, а затем реализуйте методы textFieldDidBeginEditing и textFieldDidEndEditing в вашем контроллере, чтобы определить, когда начинаются/заканчиваются редактирование.

- (void)textFieldDidBeginEditing:(UITextField *)textField{ 
    // in case you have more than one text fields in the same view 
    if(textField == self.YOUR_FIELD_NAME) 
     // change the web view height here, you can also animate it using UIView beginAnimations 
     CGRect frame = self.webView.frame; 
     frame.size.height = 200; 
     self.webView.frame = frame; 
} 


    - (void)textFieldDidEndEditing:(UITextField *)textField{ 
    // do the opposite here 
} 
2

Для уменьшения WebView необходимо следующим:

- (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 
{  
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    keyboardFrame = [self.view convertRect:keyboardFrame fromView:self.view.window]; 

    NSTimeInterval keyboardAnimationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    UIViewAnimationOptions keyboardAnimationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16; 

    CGFloat keyboardHeight = keyboardFrame.size.height; 

    [UIView animateWithDuration:keyboardAnimationDuration delay:0 options:keyboardAnimationCurve 
    animations:^{ 
      _webView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0) 
    } 
    completion:NULL]; 
} 


- (void)keyboardWillHide:(NSNotification *)notification 
{  
    NSTimeInterval keyboardAnimationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    UIViewAnimationOptions keyboardAnimationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16; 

    [UIView animateWithDuration:keyboardAnimationDuration delay:0 options:keyboardAnimationCurve 
    animations:^{ 
      _webView.contentInset = UIEdgeInsetsZero; 
    } 
    completion:NULL]; 
} 

В случае дополнительных подвидов вы должны немного изменить этот код (добавить изменение кадров для другого подвида)

Смежные вопросы