gpt4 book ai didi

ios - UITextField:开始输入时,文本框向上弹跳,然后向下弹跳

转载 作者:行者123 更新时间:2023-12-01 17:16:07 24 4
gpt4 key购买 nike

我也用 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
})
}

}

最佳答案

我遇到了 这个问题iOS 9 .在 iOS 7 和 8 上没问题。基本上我在 Collection View 单元格中有一个 UITextField。有时,当用户完成键入和编辑结束时,文本会向上“反弹”然后再次向下回到正确的位置。非常奇怪和恼人的故障。只需进行此调整即可解决 iOS 9 上的问题,并在 iOS 7 和 8 上证明是安全的:

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

关于ios - UITextField:开始输入时,文本框向上弹跳,然后向下弹跳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32264093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com