简体   繁体   English

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

[英]How do I scroll in iOS when view is only bigger than screen when keyboard appears?

I am a fairly new iPhone developer, and I am working on an iPhone app that has a view where the user needs to enter input into multiple UITextViews. 我是一个相当新的iPhone开发人员,我正在开发一个iPhone应用程序,其中有一个用户需要输入多个UITextViews输入的视图。 There are a total of 6 UITextViews and when the view appears all the text views are visible without the need to scroll. 总共有6个UITextViews,当视图出现时,所有文本视图都可见而无需滚动。 But when the user clicks on the first text view to enter the text, the last 2 text views become hidden by the keyboard and I can't figure out how to add scrolling capability so the user will be able to scroll when the keyboard is visible. 但是当用户点击第一个文本视图以输入文本时,最后2个文本视图会被键盘隐藏,我无法弄清楚如何添加滚动功能,以便用户可以在键盘可见时滚动。 I am using a UIScrollView but currently have no code to make it work since I have tried multiple different things I have found online and none have worked. 我正在使用UIScrollView,但目前没有代码可以使它工作,因为我尝试了多种不同的东西,我在网上找到,没有一个有效。 This may be an easy solution, but I am just out of ideas and have been stuck for a while. 这可能是一个简单的解决方案,但我只是出于想法而已经被困住了一段时间。 Any advice is greatly appreciated. 任何意见是极大的赞赏。 Thanks 谢谢

More Info: I am using the latest version of Xcode, developing for iPhone versions 6.1 and above. 更多信息:我正在使用最新版本的Xcode,为iPhone 6.1及更高版本开发。 I used the Interface Builder to set up the ScrollView and the AutoLayout box is checked. 我使用Interface Builder设置ScrollView并选中AutoLayout框。

-(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];

}

above 80 i was defined because my requirement is to take textview that much up when keyboard appears you can put a value which is suitable to your requirements 80以上是我定义的,因为我的要求是在键盘出现时采用textview,你可以放一个适合你要求的值

in your view did load write following lines 在你的视图中加载写下面的行

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

Make following methods. 制作以下方法。

-(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;
}

Follow 2 simple steps as following 按照以下2个简单步骤操作

  1. From storyboard/xib, adjust the frame for your scrollview and keep height as screen size. 从storyboard / xib中,调整滚动视图的框架并将高度保持为屏幕大小。

  2. Within your viewcontroller apply the contentsize for your view like, 在你的viewcontroller中为你的视图应用contentsize,比如

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

You will be able to see your entire scrollview on scrolling. 滚动时,您将能够看到整个滚动视图。

Use following link to move textview or textfield up and down automatically when keyboard appears 使用以下链接可在键盘出现时自动上下移动文本视图或文本字段

https://github.com/michaeltyson/TPKeyboardAvoiding https://github.com/michaeltyson/TPKeyboardAvoiding

this link contains demo project. 此链接包含演示项目。 you can use this as you requirement 你可以根据需要使用它

i hope this will help you. 我希望这能帮到您。

You could do it this way: 你可以这样做:

// 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);
    }]; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 键盘出现时如何滚动 UIScrollView? - How do I scroll the UIScrollView when the keyboard appears? 如何在键盘出现时使视图控制器滚动到文本字段 - How to make the view controller scroll to text field when keyboard appears NativeScript - ios 出现键盘时如何调整视图 - NativeScript - ios how to adjust view when keyboard appears IOS / Xcode:键盘出现时如何向上移动查看 - IOS/Xcode: How to Move View Up when Keyboard appears 如何在iOS(Cordova)上显示键盘时调整Web视图的大小 - How to resize web view when keyboard appears on iOS (Cordova) 键盘出现时调整视图大小(iOS) - Resize a view when a keyboard appears (iOS) iOS:滚动视图仅在键盘出现后才有效 - iOS : Scroll view works only after keyboard appears 当键盘显示UITextView而不是同一视图上的UITextField时,滚动视图 - Scroll view when keyboard appears for UITextView, but not for the UITextField that's on the same view 如何在视图出现的同时显示键盘并向上移动 UIView? - How do I display the keyboard and move up the UIView at the same time when the view appears? 当键盘出现时,如何保持UITableView在视图中? - How can I keep UITableView in view when keyboard appears?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM