【问题标题】:Adjusting Height of UITextView Animated调整 UITextView Animated 的高度
【发布时间】:2014-09-25 04:15:30
【问题描述】:

我正在尝试在键盘显示上调整 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)
    }

【问题讨论】:

  • 我已经运行了你的代码,但我没有看到这样的行为(这里提到了另一个问题stackoverflow.com/questions/26004080/…。如果没有动画,这是否会发生,只需执行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)
  • @abinop 是的。高度仍然恢复到原来的高度。

标签: objective-c swift uitextview uiviewanimation


【解决方案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!)
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多