2010-10-04 86 views
7

對於我爲iPad設計的應用程序,我有一個帶有一些文本字段/文本視圖的滾動視圖。爲了保持一切可見,我調整滾動視圖的contentSize屬性以在底部添加一個緩衝區,該緩衝區對應於鍵盤與滾動視圖重疊的程度。下面的代碼(有一些應用程序特定的東西在這裏,但我希望沒有這麼多,你無法理解它):旋轉後獲得iPad屏幕鍵盤的大小

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
    [nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
    [nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
} 


- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
    [nc removeObserver:self name:nil object:nil]; 
} 

- (void)keyboardWillShow:(NSNotification *)aNotification 
{ 
    NSValue *animationCurve = [[aNotification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey]; 
    UIViewAnimationCurve curve; 
    [animationCurve getValue:&curve]; 

    NSValue *animationDuration = [[aNotification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    NSTimeInterval duration; 
    [animationDuration getValue:&duration]; 

    NSValue *endingFrame = [[aNotification userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGRect frame; 
    [endingFrame getValue:&frame]; 

    [UIView beginAnimations:@"keyboardWillShow" context:bodyView]; 
    [UIView setAnimationCurve:curve]; 
    [UIView setAnimationDuration:duration]; 

    // Re-draw code here. 

    [UIView commitAnimations]; 
} 

- (void)keyboardWillHide:(NSNotification *)aNotification 
{ 

    NSValue *animationCurve = [[aNotification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey]; 
    UIViewAnimationCurve curve; 
    [animationCurve getValue:&curve]; 

    NSValue *animationDuration = [[aNotification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    NSTimeInterval duration; 
    [animationDuration getValue:&duration]; 

    [UIView beginAnimations:@"keyboardWillHide" context:bodyView]; 
    [UIView setAnimationCurve:curve]; 
    [UIView setAnimationDuration:duration]; 

    // Re-draw code here 

    [UIView commitAnimations];  
} 

我的問題是:我該怎麼旋轉過程中做一下鍵盤的大小? iPad旋轉時我沒有收到任何鍵盤通知,但鍵盤的大小有明顯變化。理想情況下,我只是通過鍵盤在橫向模式下重疊的量來調整contentSize屬性的高度,但我無法看到一種很好的方法,無需在兩個方向上對鍵盤的高度進行硬編碼,而我不會不想做。

回答

16

我意外地發現了這個調試的其他答案。事實證明,當iPad從縱向旋轉到橫向時,portait鍵盤在風景鍵盤出現之前隱藏(併發送其通知)(併發送通知)。所以只要你說明了這一點,你沒問題。