2016-04-23 55 views
1

我有一個帶有textview和按鈕的視圖,就像在任何典型的消息應用程序中一樣。當鍵盤出現時,文本視圖和按鈕也被向上推,但是內容被向上推離屏幕,在那裏我沒有看到它的頂端,但當我強制向下滾動時,我看到它爬下來,它向上彈當我放手的時候。我已經用快速草圖說明了我遇到的問題,第三個屏幕是所需的行爲。如何在軟件鍵盤向上推動時更改UIScrollView內容的高度

我在.xib文件中有這個視圖。我有一個觀點 - >滾動型 - >內容查看(其中有一個表,並TextView的和按鈕)

enter image description here

我註冊鍵盤的通知,這裏是爲顯示/隱藏方法的代碼

- (void)registerForKeyboardNotifications { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWasShown:) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillBeHidden:) 
              name:UIKeyboardWillHideNotification 
              object:nil]; 

} 

- (void)keyboardWasShown:(NSNotification *) aNotification { 
    NSDictionary *info = [aNotification userInfo]; 

    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 

    // the hardcoded 49 is the height of the uitabbar 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 

    [self.view addGestureRecognizer:self.tapRecognizer]; 

} 


- (void)keyboardWillBeHidden:(NSNotification *) aNotification { 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 

    [self.view removeGestureRecognizer:self.tapRecognizer]; 
} 

回答

0

試試這個。

// Called when the UIKeyboardWillShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification{ 

    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0.0, kbSize.height, 0.0); 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 

    CGPoint scrollPoint = CGPointMake(0, scrollView.contentSize.height - scrollView.bounds.size.height + scrollView.contentInset.bottom); 
    [scrollView setContentOffset:scrollPoint animated:true]; 

} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0); 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 
} 
+0

不幸的是沒有爲我工作。文本視圖和按鈕隨滾動視圖一起移動。 – ioskaveen

+0

請嘗試編輯答案 –

+0

這也行不通。我想要做的是適合軟件鍵盤之間的導航欄下的視圖頂部的滾動視圖 – ioskaveen

1

我已經看到了keyboardWasShown方法這一行

UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0); 

你在哪裏頂邊插圖是-ve,becouse 49 - keyboardSize(這是APPX 256)。用此替代它:

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height-49), 0.0); 

玩計算最佳套件給你。希望這項工作:)

相關問題