当向上推软件键盘时,如何更改UIScrollView内容的高度

时间:2016-04-23 01:15:41

标签: ios objective-c cocoa-touch uiscrollview

我有一个带有textview和按钮的视图,就像在任何典型的消息传递应用程序中一样。当键盘出现时,文本视图和按钮也被向上推,但内容被向上推离屏幕,我看不到它的顶端,但是当我强行向下滚动时,我看到它向下滑动它当我放手时,它向上弹起。我已经用快速草图说明了我遇到的问题,第三个屏幕是所需的行为。

我在.xib文件中有这个视图。我有一个观点 - > ScrollView - > contentView(有一个表格和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];
}

2 个答案:

答案 0 :(得分:1)

我在keyboardWasShown方法

中看到了这一行
UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0);

你的顶部边缘插入是-ve,因为49 - keyboardSize(这是appx 256)。替换为:

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

为您计算最佳套房。希望这项工作:)

答案 1 :(得分: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;
}