繁体   English   中英

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

[英]Enable scrolling content when keyboard appears in iOS

我在iPhone上有一个应用程序,该应用程序在viewcontroller中有很多内容。 通常,主要内容是表格。 我的问题是我举起键盘并想输入一些值来形成字段,许多内容是不可见的。 我想在出现键盘时滚动此内容。 我怎样才能做到这一点? 我的视图没有滚动视图,但是我尝试这样做,但是它不起作用。

到目前为止,我发现的所有提示都与如何放置用过的文本框有关,但这不是我的假设。

这就是我所做的,当键盘像这样向上和向下升降时,我更改了最大约束条件-

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

您可以使用以下代码以编程方式在键盘出现时更改视图中的位置:

func keyboardWasShown(notification: NSNotification) {

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

此代码将在键盘出现后将视图转换为您在.setContentOffset调用中指定的坐标。 您仍然必须获取坐标,但通常从情节提要中获取坐标非常简单。

  1. 将所有视图放入滚动视图
  2. 从滚动视图的底部到超级视图的底部设置自动布局约束
  3. viewWillAppear

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

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

在viewDidAppear中添加以下通知

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

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

并在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];
        }


    }
}

在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];
    }
}

这里yDeault是文本字段y坐标值

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

一旦显示了键盘,主视图将被向上移动,而一旦取消键盘,主视图将被移回到原始位置。

好。 我做到了 我只需要添加滚动视图,但是在我的应用程序中可能必须重构所有视图。

无需添加scrollview。 您可以直接执行此操作。 您可以更改self.view.frame无需滚动型的。

暂无
暂无

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

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