我有一个 swift 应用程序,其中 texfield 位于屏幕底部,当用户尝试输入任何内容时,键盘会覆盖文本字段,因此用户无法看到他/她正在输入的内容。为了解决这个问题,我想从用户那里获取键盘输入,每次敲击/“类型”(每次用户按下一个键),然后将其显示到位于键盘正上方的视图上。如果这不可能或非常复杂,我们非常欢迎您提出替代解决方案。
4 回答
您需要注册为通知观察者以查看键盘何时出现和消失。然后,您将在显示时将视图向上移动,或在隐藏时将其恢复为原始视图。
我的实现将整个视图向上移动键盘高度,但如果你愿意,你可以向上移动 UITextField。
您可以查看本教程,或在答案中查看我的实现:
http://www.ioscreator.com/tutorials/move-view-behind-keyboard-ios8-swift
添加观察者viewDidAppear
override func viewWillAppear(animated:Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
然后,您需要将视图移入的方法keyboardWillShow
和keyboardWillHide
新方法。
@objc func keyboardWillShow(_ notification: NSNotification) {
let info = notification.userInfo!
let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let keyboardHeight:CGFloat = keyboardSize.height
//let animationDuration:CGFloat = info[UIResponder.keyboardAnimationDurationUserInfoKey] as! CGFloat
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
self.view.transform = CGAffineTransform(translationX: 0, y: -keyboardHeight)
}, completion: nil)
}
并将视图移动到原始状态:
@objc func keyboardWillHide(_ notification: NSNotification) {
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
self.view.transform = CGAffineTransform.identity
}, completion: nil)
}
最后移除观察者viewDidDisappear
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
为了配合 Stefan Salatics 的回答,我使用以下代码,因为键盘通常仍会覆盖文本字段。
-(void) keyboardWillShow:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];
// Start animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// Get Keyboard height and subtract the screen height by the origin of the textbox and height of text box to position textbox right above keyboard
self.scrollView.contentOffset = CGPointMake(0,keyboardBounds.size.height-([UIScreen mainScreen].bounds.size.height - commentBox.frame.origin.y - commentBox.frame.size.height));
[UIView commitAnimations];
}
以下是我如何解决这个问题。我希望这个方法能解决你的问题。
这是您要使用的文本字段。
@IBOutlet var userID : UITextField!
你需要编写这个函数。
func textFieldShouldReturn(textField: UITextField!)-> Bool
{ userID.resignFirstResponder( )
return true
}
在您想要的函数中,您需要编写此语法。
@IBAction func login(sender: AnyObject)
{
userID.resignFirstResponder( )
}
这是在 ViewDidLoad 或 ViewDidAppear 中
override func viewDidLoad( )
{
userID.delegate = self;
}
我希望这对你有用。
尽管 Stefan 的回答更适合您当前的实施。您的问题是如何获取用户键入的每个笔划。
为了完成
class viewController: UIViewController, UITextFieldDelegate{
var textField = UITextField()
override func viewDidLoad() {
textField.delegate = self
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
//string is used here as the value of the textField.
//This will be called after every user input in the textField
}
}
string
可以用来查看最新的用户输入是什么。您可以使用当前值存储一个局部变量,并将其与“新”值进行比较以查看已更改的内容。