当键盘出现时视图仅大于屏幕时,如何在iOS中滚动?

时间:2013-11-01 09:38:36

标签: ios iphone uiscrollview keyboard

我是一个相当新的iPhone开发人员,我正在开发一个iPhone应用程序,该应用程序具有用户需要在多个UITextView中输入输入的视图。总共有6个UITextViews,当视图出现时,所有文本视图都可见而无需滚动。但是当用户点击第一个文本视图以输入文本时,最后2个文本视图会被键盘隐藏,我无法弄清楚如何添加滚动功能,以便用户可以在键盘可见时滚动。我正在使用UIScrollView,但目前没有代码可以使它工作,因为我尝试了多种不同的东西,我在网上找到,没有一个有效。这可能是一个简单的解决方案,但我只是出于想法而已经被困住了一段时间。任何意见是极大的赞赏。谢谢

更多信息:我正在使用最新版本的Xcode,为iPhone 6.1及更高版本开发。我使用Interface Builder设置ScrollView并选中AutoLayout框。

5 个答案:

答案 0 :(得分:2)

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{
    /* this returns the keyboard when user click on return button which is reside on keyboard */

    if([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        [yourscrollview setContentOffset:CGPointMake(0,0)animated:YES];
    }
    return YES;
}

-(void)textViewDidEndEditing:(UITextView *)textView

{

 /* it used for hide keyboard and set all control on their original position */

}

-(void)textViewDidBeginEditing:(UITextView *)textView

{

 /* depending upon condition it will scroll the textview so textview can't reside behind the keyboard */

   [yourscrollview setContentOffset:CGPointMake(0,textView.center.y-80)animated:YES];

}

80以上是我的定义,因为我的要求是当键盘出现时,你可以根据你的要求设置一个非常适合的textview

答案 1 :(得分:2)

在您的视图中

确实加载了以下行

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

制作以下方法。

-(void)keyboardDidHide
{
    scrollview.frame = YOUR_ORIGINAL_FRAME;//You should set frame when keyboard is not there
    scrollview.contentSize=scrollview.frame.size;
}
-(void)keyboardDidShow
{
    CGRect r = scrollview.frame;
    scrollview.contentSize=scrollview.frame.size;
    r.size.height - = 216;//216 is keyboard height for iPhone.
    scrollview.frame = r;
}

答案 2 :(得分:0)

按照以下2个简单步骤进行操作

  1. 从storyboard / xib中调整滚动视图的框架并保留 高度作为屏幕尺寸。

  2. 在viewcontroller中,为视图应用contentsize,如

            <scrollview>.contentSize=CGSizeMake(320, 700);
    
  3. 您可以在滚动时看到整个滚动视图。

答案 3 :(得分:0)

使用以下链接可在键盘出现时自动上下移动文本视图或文本字段

https://github.com/michaeltyson/TPKeyboardAvoiding

此链接包含演示项目。你可以根据需要使用它

我希望这会对你有所帮助。

答案 4 :(得分:0)

你可以这样做:

// In View First add keyboard appearance and disappearance Notifications

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

// Now inside this selector method
-(void)keyboardWillShow:(NSNotification *)notify
{
    if (!notify) {
        return;
    }

    NSDictionary *userInfo = [notify userInfo];

    NSValue *keyboardEndFrame = ([userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]);

    CGRect endFrame = keyboardEndFrame.CGRectValue;

    // Change the frame of the view to its parent
    CGRect loginViewFrame = [loginView.superview convertRect:loginView.frame fromView:loginView];

    // Check the keyboard covers the view 
    if (CGRectGetMinY(endFrame) < CGRectGetMaxY(loginViewFrame))
    {
        // If YES calculate Distance. Save this difference to animate back the view 
        difference = CGRectGetMaxY(loginViewFrame)- CGRectGetMinY(endFrame);

        // animate that View
        [self animateViewUp:YES withMovementDistance:difference];
    }
}

// inside Will Hide
-(void)keyboardWillHide
{
    // Animate back the view with the calculated distance
    [self animateViewUp:NO withMovementDistance:difference];
}

- (void)animateViewUp:(BOOL)up withMovementDistance:(int)movementDistance
{
    const float movementDuration = 0.3f;

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

    [UIView animateWithDuration:movementDuration animations:^{
    loginView.frame = CGRectOffset(loginView.frame, 0, movement);
    }]; 
}