Ios 显示键盘时调整UITextView的大小

Ios 显示键盘时调整UITextView的大小,ios,objective-c,uitextview,Ios,Objective C,Uitextview,我想在键盘出现时调整文本视图的大小。下面是我的代码。我打开了自动布局,因此使用了来自superview的textView->bottom space约束,并通过IBOutlet distance frombottom引用它 - (void)keyboardWillShow:(NSNotification *)notification { [UIView animateWithDuration:0.3 animations:^{ NSDictionary* d = [notificat

我想在键盘出现时调整文本视图的大小。下面是我的代码。我打开了自动布局,因此使用了来自superview的textView->bottom space约束,并通过IBOutlet distance frombottom引用它

- (void)keyboardWillShow:(NSNotification *)notification
{
  [UIView animateWithDuration:0.3 animations:^{
    NSDictionary* d = [notification userInfo];
    CGRect r = [d[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    r = [textView convertRect:r fromView:Nil];
    if(IS_IPHONE_6||IS_IPHONE_6P)
      distanceFromBottom.constant = r.origin.y+78;
    else if(IS_IPHONE_5)
      distanceFromBottom.constant = r.origin.y+183;
  }];
}

上面的代码工作得很完美。我不明白的是,为什么我需要为iPhone6添加+78或为iPhone5添加183。这两个价值观是我反复尝试的结果。如果我不添加这些,textView将扩展到键盘下方。请帮我解开这个谜团。

视图将出现的方法中,添加以下内容:

- (void) viewWillAppear:(BOOL)paramAnimated{
    [super viewWillAppear:paramAnimated];

    [[NSNotificationCenter defaultCenter] 
        addObserver:self 
           selector:@selector(handleKeyboardDidShow:) 
               name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter]
        addObserver:self
           selector:@selector(handleKeyboardWillHide:)     
               name:UIKeyboardWillHideNotification object:nil];    
}
然后实现通知中心的两种方法,如下所示:

- (void) handleKeyboardDidShow:(NSNotification *)paramNotification{

    NSValue *keyboardRectAsObject =
        [[paramNotification userInfo] 
            objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = CGRectZero;
    [keyboardRectAsObject getValue:&keyboardRect];

    yourTextView.contentInset =
        UIEdgeInsetsMake(0.0f,
                         0.0f,
                         keyboardRect.size.height,
                         0.0f);
}
另一种是:

- (void) handleKeyboardWillHide:(NSNotification *)paramNotification{

    yourTextView.contentInset = UIEdgeInsetsZero;
}

它将适用于所有设备;)

Swift/修改版

使用上述方法,我进行了一些调整,以在键盘显示和隐藏时使用
NSLayoutConstraint
更改height
constant
属性。如果旋转设备,这也可以工作

1。设置文本视图约束

然后控制从高度约束拖动一个出口到类

2。添加以下内容

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(SkillDescriptionViewController.keyboardWillShowHandle(_:)), name: UIKeyboardDidShowNotification, object: nil)

        NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(SkillDescriptionViewController.keyboardWillHideHandle), name: UIKeyboardWillHideNotification, object: nil)

    }


    func keyboardWillShowHandle(note:NSNotification) {
        guard let keyboardRect = note.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
        let kbFrame = keyboardRect.CGRectValue()
        tvHeight.constant = -kbFrame.height
        view.layoutIfNeeded()
    }

    func keyboardWillHideHandle() {
        tvHeight.constant = 0
        view.layoutIfNeeded()
    }

基于上述答案的Swift 5解决方案

通知API已更改(20190707)以及如何使用选择器

当键盘弹出窗口覆盖UITextView,导致用户无法查看已编辑的文本时,可以使用此处的解决方案

import UIKit

class DCNodeLogEntryViewController: UIViewController {
    @IBOutlet weak var textViewNodeLogEntry: UITextView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        NotificationCenter.default.addObserver(
            self,
            selector: #selector(DCNodeLogEntryViewController.handleKeyboardDidShow(notification:)),
            name: UIResponder.keyboardDidShowNotification,
            object: nil
        )

        NotificationCenter.default.addObserver(
            self,
            selector: #selector(DCNodeLogEntryViewController.handleKeybolardWillHide),
            name: UIResponder.keyboardWillHideNotification,
            object: nil
        )
    }

    @objc func handleKeyboardDidShow(notification: NSNotification) {
        guard let keyboardRect = notification
            .userInfo![UIResponder.keyboardFrameEndUserInfoKey]
            as? NSValue else { return }

        let frameKeyboard = keyboardRect.cgRectValue

        textViewNodeLogEntry.contentInset = UIEdgeInsets(
            top: 0.0,
            left: 0.0,
            bottom: frameKeyboard.size.height,
            right: 0.0
        )

        view.layoutIfNeeded()
    }

    @objc func handleKeybolardWillHide() {
        textViewNodeLogEntry.contentInset = .zero
    }
}

真棒,先生。工作很有魅力。:)