Ios 键盘在UITextView出现时隐藏它

Ios 键盘在UITextView出现时隐藏它,ios,objective-c,xcode,cocoa-touch,swift,Ios,Objective C,Xcode,Cocoa Touch,Swift,我已将我的UIkeyboarddismissmode设置为.Interactive,我不知道如何调整UITextView的大小,以便键盘不会覆盖内容。我还有一个UIToolbar作为inputaccessory 这是我的密码 @IBOutlet weak var textView: UITextView! override func viewDidLoad() { super.viewDidLoad() self.addDoneButtonOnKeyboard()

我已将我的
UIkeyboarddismissmode
设置为
.Interactive
,我不知道如何调整
UITextView
的大小,以便键盘不会覆盖内容。我还有一个
UIToolbar
作为
inputaccessory

这是我的密码

@IBOutlet weak var textView: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.addDoneButtonOnKeyboard()

    self.textView.keyboardDismissMode = .Interactive
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)

    // Add notification about keyboard
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillHideNotification, object: nil)
    //        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardFrameDidChange:"), name:UIKeyboardWillChangeFrameNotification, object: nil)
}

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(true)
    NSNotificationCenter.defaultCenter().removeObserver(UIKeyboardWillShowNotification)
    NSNotificationCenter.defaultCenter().removeObserver(UIKeyboardWillHideNotification)
}


// The UIToolbar

var toolbar = UIToolbar()

func addDoneButtonOnKeyboard()
{
    var doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 44))
    doneToolbar.barStyle = UIBarStyle.Default

    var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    var photo: UIBarButtonItem = UIBarButtonItem(title: "Add Photo", style: UIBarButtonItemStyle.Bordered, target: self, action: Selector("photoButtonAction"))
    var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("doneButtonAction"))

    var items = NSMutableArray()
    items.addObject(photo)
    items.addObject(flexSpace)
    items.addObject(done)

    doneToolbar.items = items
    doneToolbar.sizeToFit()

    doneToolbar.tintColor = UIColor(red: 240/225, green: 42/225, blue: 20/225, alpha: 1)
    doneToolbar.translucent = true

    self.toolbar = doneToolbar
    self.textView.inputAccessoryView = self.toolbar
}

func keyboardFrameDidChange(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        var endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
        var beginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()

        let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
        let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
        let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)

        var animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0

        var newFrame = self.textView.frame
        var keyboardFrameEnd = self.view.convertRect(endFrame!, toView: nil)
        var keyboardFrameBegin = self.view.convertRect(beginFrame!, toView: nil)

        newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y)
        self.textView.frame = newFrame

        UIView.animateWithDuration(animationDuration,
            delay: NSTimeInterval(0),
            options: animationCurve,
            animations: { self.view.layoutIfNeeded() },
            completion: nil)
    }

}
上面的代码只会让
textView
以一种奇怪的方式移动,我不知道如何实现像消息应用程序那样的效果

更新:

func keyboardFrameEnd(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        var endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()

        // Change contentInset
        self.textView.contentInset.bottom = endFrame!.height
        self.textView.scrollIndicatorInsets.bottom = endFrame!.height
    }
}

我把代码改为上面的。但有时当我把键盘松开一半时,scrollView会突然弹到底部,这很奇怪。有什么建议吗?

UITextView是一个滚动视图。当键盘出现时,您不需要调整其大小:您应该调整它的contentInset属性,将.bottom增加键盘的高度。

它可以应用于是否有tabbar。 它将完美地将通过键盘移动的视图恢复到原始位置

// Set this up in storyboard first. 
// It's a vertical spacing constraint between your text view and bottom of superview.
@IBOutlet weak var bottomSpacingConstraint: NSLayoutConstraint! 

override func viewDidLoad() {
        super.viewDidLoad()            

        //    Receive(Get) Notification
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillHideNotification, object: nil)


        self.originalConstraint = self.keyboardHeightLayoutConstraint?.constant //for original coordinate.
}

func keyboardNotification(notification: NSNotification) {
        let isShowing = notification.name == UIKeyboardWillShowNotification

        var tabbarHeight: CGFloat = 0
        if self.tabBarController? != nil {
            tabbarHeight = self.tabBarController!.tabBar.frame.height
        }
        if let userInfo = notification.userInfo {
            let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
            let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
            let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
            let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
            let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
            self.keyboardHeightLayoutConstraint?.constant = isShowing ? (endFrame!.size.height - tabbarHeight) : self.originalConstraint!
            UIView.animateWithDuration(duration,
                delay: NSTimeInterval(0),
                options: animationCurve,
                animations: { self.view.layoutIfNeeded() },
                completion: nil)
        }
}

试试这个:。我知道这是Objective-C代码,但您可以将其改编为Swift。看看这个,我试过上面的方法,但是它们都是用动画硬编码的。My keyboarddissmissmode设置为交互式,这意味着您可以手动向下拖动键盘,而不是像iMessage应用程序那样设置动画。谢谢。部分达到了预期效果。然而,右边的滚动条没有改变,这是令人困惑的。有什么方法可以解决这个问题吗?用ScrollIndicationSets.bottom解决了这个问题。谢谢这就是想法:滚动视图延伸到键盘后面,在底部添加一个插入,这样你可以向下滚动,看到文本的底部。但是我发现了另一个bug。当内容很短时,例如文本视图中只有两行,您不能交互地关闭键盘。此外,当内容很短,您可以在屏幕上查看所有内容而无需滚动时,当您关闭键盘时,textView会突然向上反弹,然后在关闭键盘后恢复到原始状态。这看起来很奇怪。