UITextField:开始输入时,文本字段反弹,然后反弹

时间:2015-08-28 05:44:23

标签: ios uitextfield uitextview

我也用UITextView看过这个。显然,我遗漏了一些非常基本的东西。

我试过了: - 将文本大小设置为小 - UITextField:设置1行,不调整大小 - 删除移动texfield的功能

import UIKit

class XViewController: UIViewController, UITextFieldDelegate, UIGestureRecognizerDelegate {

    // UI elements
    let cancelButton = UIButton()
    let okButton = UIButton()
    var image:UIImage?
    var previewImageView:UIImageView = UIImageView()
    let textField = UITextField()
    let tapGestureRecognizer = UITapGestureRecognizer()
    let textFieldTypingPositionY:CGFloat = 200
    var textFieldActualPositionY:CGFloat!
    let textFieldHeight:CGFloat = 40

    var draggingTextField:Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupTextField()
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString text: String) -> Bool {
        let textFieldSize = textField.text!.characters.count
        let textSize = text.characters.count
        if text == "\n" && textFieldSize == 0 {
            // press return on empty textfield
            textField.text = ""
            hideTextField()
            return true
        } else if textFieldSize == 1 && text == "" {
            // backspace to empty
            textField.text = ""
            hideTextField()
            return true
        } else if text == "\n"  {
            textField.resignFirstResponder()
        } else if (textFieldSize + textSize) > 30 {
            let diff = (textFieldSize - textSize) - 30
            textField.text = (text as NSString).substringToIndex(diff - 1)
        }
        return true
    }

    func setupTextField() {
        // TextView
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
        textField.textColor = UIColor.whiteColor()
        textField.font = UIFont.systemFontOfSize(14.0)
        textField.textAlignment = NSTextAlignment.Center
        textField.text = ""
        textField.hidden = true
        textField.delegate = self
        self.view.addSubview(textField);

        // Text view constraints
        let leftConstraint = textField.leftAnchor.constraintEqualToAnchor(view.leftAnchor)
        let rightConstraint = textField.rightAnchor.constraintEqualToAnchor(view.rightAnchor)
        let heightConstraint = textField.heightAnchor.constraintEqualToConstant(textFieldHeight)
        let verticalConstraint = textField.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 200)
        NSLayoutConstraint.activateConstraints([leftConstraint, rightConstraint, heightConstraint, verticalConstraint])

        // Tap gesture recognizer; if no text view is visible, shows text view at vertical location of tap
        tapGestureRecognizer.delegate = self
        tapGestureRecognizer.addTarget(self, action: "tapGesture:")
        view.addGestureRecognizer(tapGestureRecognizer)
    }

    func tapGesture(rec:UITapGestureRecognizer) {
        // Show the textView in a location?
        if textField.hidden == false {
            if (textField.text!.characters.count == 0) {
                hideTextField()
            } else {
                restoreTextFieldPosition()
            }
        } else {
            moveTextFieldToEditingPosition()
        }
    }


    func moveTextFieldToEditingPosition() {
        // Textview animates into position a la snapchat
        self.textField.userInteractionEnabled = false
        textField.hidden = false
        UIView.animateWithDuration(0.2, animations: { () -> Void in
            self.textField.center.y = self.textFieldTypingPositionY
            }, completion: { (Bool) -> Void in
                self.textField.userInteractionEnabled = true
                self.textField.becomeFirstResponder()
        })
    }

    func hideTextField() {
        // Hide text view and reset position
        textField.resignFirstResponder()
        textField.userInteractionEnabled = false
        UIView.animateWithDuration(0.2, animations: { () -> Void in
            }, completion: { (Bool) -> Void in
                self.textField.hidden = true
        })
    }



    func restoreTextFieldPosition() {
        // Restore original position of textview after exiting keyboard
        self.textField.userInteractionEnabled = false
        textField.resignFirstResponder()
        UIView.animateWithDuration(0.2, animations: { () -> Void in
            }, completion: { (Bool) -> Void in
                self.textField.userInteractionEnabled = true
        })
    }

}

3 个答案:

答案 0 :(得分:10)

iOS 9 我遇到了这个问题。它在iOS 7和8上都没问题。基本上我在集合视图单元格中有一个UITextField。有时当用户完成输入和编辑结束时,文本"弹跳"然后再次下降到正确的位置。非常奇怪和烦人的故障。只需进行此调整即可解决iOS 9上的问题,并证明在iOS 7和8上是安全的:

 - (void)textFieldDidEndEditing:(UITextField *)textField
 {
    [textField layoutIfNeeded]; //Fixes iOS 9 text bounce glitch
    //...other stuff
 }

答案 1 :(得分:1)

这是textField的奇怪行为。我有同样的问题,原因是你正在改变textField的位置。

<强>解决方案

无论你使用uiview.animation改变位置(在你的情况下为Y位置)编写的代码....删除此代码

当你移开焦点或切换到另一个控件时,我确定会发生这种反弹。

最后,删除动画代码或保留此反弹动画(不错)。

:)

答案 2 :(得分:1)

不确定你是否解决了这个问题,但对我来说这是因为我的UITextFieldDelegate中有一段代码调用layoutIfNeeded - 这导致重新计算所有自动布局约束。

我删除了layoutIfNeeded,似乎修复了弹跳