簡體   English   中英

出現鍵盤時如何使滾動視圖在縮小的視圖區域中滾動

[英]How to make the scrollview scrollable in reduced view area when keyboard appears

我已經將所有視圖嵌入到xib的UIScrollView中。 滾動視圖內容覆蓋狀態欄下方的所有屏幕。 現在,當點擊文本字段時,我可以將scrollview稍微向上移動。 但我希望它可以完全滾動,直到在鍵盤上方也可以看到最底部的視圖。 同樣,當scrollview滾動到top時,它應該到達正常的原始位置。 因此,總的來說,我想要一個完全可滾動的功能,就像上面為我的滾動視圖所提到的那樣。

我已經完成了以下技巧,但沒有運氣:

技巧1:更改scrollview的高度,以使內容大於scrollview的高度,因此該視圖可滾動:

-(void)keyboardWillAppear:(NSNotification *)sender
{
CGFloat y_offset=0;

if([UIScreen mainScreen].bounds.size.height == 480){
    y_offset = 80;
} else {
    y_offset = 70;
}
NSDictionary* userInfo = [sender userInfo];

CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
keyboardHeight = keyboardEndFrame.size.height;

[UIView animateWithDuration:0.5f animations:^{
    [self.view setFrame:CGRectMake(0, - y_offset, self.view.frame.size.width, self.view.frame.size.height)];
}];


[self.loginScrollView setFrame:CGRectMake(self.loginScrollView.frame.origin.x, self.loginScrollView.frame.origin.y, self.loginScrollView.frame.size.width, [UIScreen mainScreen].bounds.size.height - keyboardHeight)];
}

-(void)keyboardWillDisappear:(NSNotification *)sender
{
[UIView animateWithDuration:0.5f animations:^{
    [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}];
[self.loginScrollView setFrame:CGRectMake(self.loginScrollView.frame.origin.x, self.loginScrollView.frame.origin.y, self.loginScrollView.frame.size.width, [UIScreen mainScreen].bounds.size.height)];
}

技巧2:根據其他建議,我更改了UIScrollView的contentInset。

在keyboardWillAppear方法中,我添加了以下代碼:

  CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
  UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+100, 0.0);
  self.loginScrollView.contentInset = contentInsets;
  self.loginScrollView.scrollIndicatorInsets = contentInsets;

並在keyboardWillDisappear方法中將contentInset設置回零值。

因此,請告訴我是否需要其他任何方法來解決此問題,或者需要在滾動視圖框架中進行任何其他可能的更改。 而且,如果我打開了bouncesVertically功能,即使在屏幕上看到了我不想要的完整子視圖,它也可以反彈。 因此,基本上,我希望它在鍵盤不存在時凍結,並且在其向上滾動時可滾動到可見區域。 因此,還有其他建議嗎? 提前致謝。

-(void)textFieldDidBeginEditing:(UITextField *)textField

{

[self animateTextField:textField up:YES];

}

-(void)textFieldDidEndEditing:(UITextField *)textField

{

[self animateTextField:textField up:NO];

}

-(void)animateTextField:(UITextField *)textField up:(BOOL)up {

    const int movementDistance = -60; // change this size if you need
    const float movementDuration = 0.3f; // change this size if you need

    int movement = (up ? movementDistance : -movementDistance);

    [UIView beginAnimations: @"animateTextField" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

對我有用

我真的可以推薦這個庫: https : //github.com/michaeltyson/TPKeyboardAvoiding

它非常易於使用,並且適用於ScrollView,TableView和CollectionView!

從概念上講,當“ scrollView Size == scrollView ContentSize ”時,它不會滾動。 為了使其可滾動,我們需要增加contentSize 在您的問題中,您需要調整scrollView的contentSize和框架。 這可以用您的第一種方法來完成。

對於第二種方法,更改邊緣插圖將為內容可繪制區域創建一種填充。 這樣可以使底部的內容可見,但不會影響contentSize ,因此視圖將無法滾動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM