【问题标题】:Enable scrolling content when keyboard appears in iOS当键盘出现在 iOS 中时启用滚动内容
【发布时间】:2015-09-25 06:11:33
【问题描述】:

我在 iPhone 上有一个应用程序,它在 viewcontroller 中有很多内容。一般主要内容是形式。我的问题是我放了键盘,想输入一些值来形成字段,大部分内容是不可见的。我想在键盘出现时滚动这个内容。我怎样才能做到这一点? 我的视图没有滚动视图,但我尝试这样做但它不起作用。

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

【问题讨论】:

标签: ios objective-c swift uiviewcontroller


【解决方案1】:

这就是我所做的,当键盘像这样上下升降时,我改变了顶部约束-

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:
      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];
            }];
        }
        

      【讨论】:

        【解决方案4】:
        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);
                }];
        }
        

        【讨论】:

        • contanerView 是设置文本字段的视图
        【解决方案5】:

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

        一旦显示键盘,主视图将向上移动,一旦键盘关闭,主视图将移回原始位置

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

            无需添加滚动视图。您可以直接执行此操作。您可以更改self.view.frame 而无需滚动视图。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-12-09
              • 1970-01-01
              • 2012-04-19
              • 2011-09-02
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多