简体   繁体   English

当键盘出现在iOS中时启用滚动内容

[英]Enable scrolling content when keyboard appears in iOS

I have an application on iPhone who has much content in viewcontroller. 我在iPhone上有一个应用程序,该应用程序在viewcontroller中有很多内容。 Generally the main content is form. 通常,主要内容是表格。 My problem is that I put up keyboard and want to type some value to form fields much of content is invisible. 我的问题是我举起键盘并想输入一些值来形成字段,许多内容是不可见的。 I want to scroll this content when keyboard appear. 我想在出现键盘时滚动此内容。 How can I do this? 我怎样才能做到这一点? My view does'n have scroll view however I tried do this but it don't works. 我的视图没有滚动视图,但是我尝试这样做,但是它不起作用。

All hints I found so far concern how to put up used textbox, but this is not my assumption. 到目前为止,我发现的所有提示都与如何放置用过的文本框有关,但这不是我的假设。

This is what i did, I changed the top constraint when keyboard rises up and down like this- 这就是我所做的,当键盘像这样向上和向下升降时,我更改了最大约束条件-

in viewDidLoad call this method- viewDidLoad调用此方法-

   [self observeKeyboard];
#pragma mark- Keyboard Listen Methods
- (void)observeKeyboard {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardFrame = [kbFrame CGRectValue];
CGFloat height = keyboardFrame.size.height;
self.COnstraintTopViewVertical.constant=(value of constant)-height;
[UIView animateWithDuration:animationDuration animations:^{
    [self.view layoutIfNeeded];
}];
}

- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
self.ConstraintTopViewVertical.constant=reset value;
[UIView animateWithDuration:animationDuration animations:^{
    [self.view layoutIfNeeded];
}];
}

You can programmatically change position in the view upon keyboard appearance with the following code: 您可以使用以下代码以编程方式在键盘出现时更改视图中的位置:

func keyboardWasShown(notification: NSNotification) {

    UIView.animateWithDuration(0.3, animations: { () -> Void in
        self.setContentOffset(CGPoint(x: setXOffsetHere, y: setYOffsetHere), animated: true)
    })
}

This code will transition the view to the coordinates you specify in .setContentOffset call after keyboard appearance. 此代码将在键盘出现后将视图转换为您在.setContentOffset调用中指定的坐标。 You still have to get the coordinates, but usually they are pretty straightforward to get from storyboard. 您仍然必须获取坐标,但通常从情节提要中获取坐标非常简单。

  1. Put all your views in a scroll view 将所有视图放入滚动视图
  2. Set an auto layout constraint from the bottom of the scroll view to the bottom of the superview 从滚动视图的底部到超级视图的底部设置自动布局约束
  3. In viewWillAppear viewWillAppear

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; 
  4. Handle the KB notification 处理知识库文章通知

     - (void)keyboardWillChangeFrame:(NSNotification*)aNotification { CGRect kbFrame = [aNotification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect kbWindowIntersectionFrame = CGRectIntersection(self.view.window.bounds, kbFrame); [UIView animateWithDuration:[aNotification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{ self.scrollVBottomConstraint.constant = kbWindowIntersectionFrame.size.height; [self.view layoutIfNeeded]; }]; } 
what i got from your question is: you are using textfield on UIView which is not on the scrollview, now you have to scroll the UIView when keyboard appears. just do following stuff.
-Give the tag to your textfield.
-suppose tag you have given is 4545646
-do not forgot to assign delegate to your textfield (it will execute below delegate method when you start to edit)
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 4545646) {
        UIView *contanerView = [self.view viewWithTag:5556466];
        [UIView animateWithDuration:0.5 animations:^{
            contanerView.frame = CGRectMake(self.view.frame.origin.x+1, -85, self.view.frame.size.width, self.view.frame.size.height-9);
        }];
    }
}
Change the frame as per your requirement.
and 
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.tag == 4545646) {
        [textField resignFirstResponder];
        UIView *contanerView = [self.view viewWithTag:5556466];
        [UIView animateWithDuration:0.5 animations:^{
            contanerView.frame = CGRectMake(self.view.frame.origin.x+1, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
        }];
}

In viewDidAppear add the following notifications 在viewDidAppear中添加以下通知

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

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

and in keyBoardShown method get the keyboard height 并在keyBoardShown方法中获取键盘高度

 - (void)keyBoardShown:(NSNotification*)notification
{
    if(self.view.frame.origin.y>=0)
    {
        NSDictionary* keyboardInfo = [notification userInfo];
        NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
        CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];

        //    NSLog(@"hihi : %f",keyboardFrameBeginRect.size.height);

        keyBoardHeight = keyboardFrameBeginRect.size.height-yDeault+50;


        if (keyBoardHeight>0) {
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.5];

            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
            self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y-keyBoardHeight), self.view.frame.size.width, self.view.frame.size.height);
            [UIView commitAnimations];
        }


    }
}

in keyBoardDismiss setback the mainview y coordinate to previous value 在keyBoardDismiss中将主视图y坐标设置为先前值

-(void)keyBoardDismiss:(NSNotification*)notification{
    if(self.view.frame.origin.y<0){
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];

        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y+keyBoardHeight), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

here yDeault is textfield y coordinate value 这里yDeault是文本字段y坐标值

 yDeault= screenHeight-textField.frame.origin.y+textField.frame.size.height;

Once the keyboard is shown main view will be moved up and once keyboard is dismissed main view will be moved back to the original position 一旦显示了键盘,主视图将被向上移动,而一旦取消键盘,主视图将被移回到原始位置。

OK. 好。 I done this. 我做到了 I need to just add scroll view but in my app probably I have to reconstruct all my view. 我只需要添加滚动视图,但是在我的应用程序中可能必须重构所有视图。

No need to add scrollview. 无需添加scrollview。 Directly you can do this. 您可以直接执行此操作。 You can change the self.view.frame without the need of scrollview. 您可以更改self.view.frame无需滚动型的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM