[swift]调整高度 UITextView 进行动画处理

发布时间: 2015/5/4 19:52:41
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图调整大小 UITextView 键盘展出。然而,我添加的代码下面行为怪异。文本视图跳转到新的高度,然后回到原来的高度进行动画处理。在这里任何帮助将不胜感激。

 //Handles keyboard appearance
    func keyboardWillShow(notification:NSNotification){
        var rightButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("dismissKeyboard"))
        self.navigationItem.rightBarButtonItem = rightButton
        self.origNoteFrame = notes.frame
        var duration = NSTimeInterval((notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as NSNumber).intValue)
        var options = UIViewAnimationOptions(
            UInt((notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as NSNumber).integerValue << 16)
        )
        UIView.animateWithDuration(
            duration,
            delay: 0.0,
            options: options,
            animations: {
                self.notes.frame = CGRectMake(self.notes.frame.origin.x, self.notes.frame.origin.y, self.notes.frame.width, (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue().origin.y - 10)
            },
            completion: nil)
    }

解决方法 1:

我解决了这个问题通过调整滚动插入而不是 UITextView 的高度。

//Handles keyboard appearance
    func keyboardWillShow(notification:NSNotification){

        //Set up done button
        self.origRightBtn = self.navigationItem.rightBarButtonItem
        var rightButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("dismissKeyboard"))
        self.navigationItem.rightBarButtonItem = rightButton

        //Move scrolling insets
        var info:Dictionary = notification.userInfo!
        var rect:CGRect = self.view.convertRect((info[UIKeyboardFrameBeginUserInfoKey] as NSValue).CGRectValue(), fromView: nil)
        var insets:UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: rect.size.height - 150, right: 0.0)
        self.notes.contentInset = insets
        self.notes.scrollIndicatorInsets = insets
        var newRect:CGRect = self.view.frame
        newRect.size.height -= rect.height - 150

        //Scroll to selected range
        if(selectedRange != nil)
        {
            self.notes.scrollRangeToVisible(selectedRange!)
        }
    }
官方微信
官方QQ群
31647020