Делаем вид прокручиваемым при появлении клавиатуры - PullRequest
2 голосов
/ 26 мая 2011

Удалось получить его в моем первом представлении, но не работает во втором представлении.

Вот что я сделал в обоих представлениях, с небольшими отличиями в целях отладки в консоли

-(void) viewWillAppear:(BOOL)animated {
    //---registers the notifications for keyboard---
    // to see if keyboard is shown / not shown
    [[NSNotificationCenter defaultCenter]
     addObserver: self
     selector:@selector(keyboardDidShow:)
     name:UIKeyboardDidShowNotification
     object:self.view.window];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(keyboardDidHide:)
     name:UIKeyboardDidHideNotification
     object:nil];
}

//---when the keyboard appears---
-(void) keyboardDidShow:(NSNotification *) notification {
    if (keyboardIsShown) return;
    NSLog(@"Keyboard is visible 1"); // debugger purpose "Keyboard is visible 2" on the second view.

    NSDictionary* info = [notification userInfo];

    //---obtain the size of the keyboard---
    NSValue *aValue =
    [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect =
    [self.view convertRect:[aValue CGRectValue] fromView:nil];


    //---resize the scroll view (with keyboard)---
    CGRect viewFrame = [scrollview frame];
    NSLog(@"%f", viewFrame.size.height);
    viewFrame.size.height -= keyboardRect.size.height;
    scrollview.frame = viewFrame;
    NSLog(@"%f", keyboardRect.size.height);
    NSLog(@"%f", viewFrame.size.height);
    //---scroll to the current text field---
     CGRect textFieldRect = [currentTextField frame];
    [scrollview scrollRectToVisible:textFieldRect animated:YES];
    keyboardIsShown = YES;
}

//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
    NSDictionary* info = [notification userInfo];

    //---obtain the size of the keyboard---
    NSValue* aValue =
    [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect =
    [self.view convertRect:[aValue CGRectValue] fromView:nil];


    //---resize the scroll view back to the original size
    // (without keyboard)---
    CGRect viewFrame = [scrollview frame];
    viewFrame.size.height += keyboardRect.size.height;
    scrollview.frame = viewFrame;

    keyboardIsShown = NO;
}

//---before the View window disappear---
-(void) viewWillDisappear:(BOOL)animated {
    //---removes the notifications for keyboard---
    [[NSNotificationCenter defaultCenter]
     removeObserver: self
     name:UIKeyboardWillShowNotification
     object:nil];

    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:UIKeyboardWillHideNotification
     object:nil];
}

1 Ответ

1 голос
/ 26 мая 2011

Вы регистрируетесь для уведомлений UIKeyboardDidShowNotification и UIKeyboardDidHideNotification, а затем отменяете регистрацию для уведомлений UIKeyboardWillShowNotification и UIKeyboardWillHideNotification. Там твоя ошибка.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...