1

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

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

4

5 回答 5

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;
}
于 2013-11-01T09:54:48.970 回答
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 提升很多,您可以输入一个适合您要求的值

于 2013-11-01T09:49:38.090 回答
0

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

https://github.com/michaeltyson/TPKeyboardAvoiding

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

我希望这能帮到您。

于 2013-11-01T10:17:17.217 回答
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);
    }]; 
}
于 2014-10-16T08:35:30.073 回答
0

按照以下2个简单步骤

  1. 从情节提要/xib,调整滚动视图的框架并将高度保持为屏幕尺寸。

  2. 在您的视图控制器中为您的视图应用内容大小,例如,

            <scrollview>.contentSize=CGSizeMake(320, 700);
    

您将能够在滚动时看到整个滚动视图。

于 2013-11-01T09:46:21.850 回答