我在swift中有一个应用程序,其中texfield位于屏幕的底部,当用户尝试键入任何内容时,键盘覆盖文本字段,因此用户无法看到他/她正在键入的内容。为了解决这个问题,我希望得到用户的键盘输入,每个笔画/"类型" (每次用户按下一个键),然后将其显示在位于键盘正上方的视图上。如果这不可能或非常复杂,欢迎您提出替代解决方案。
答案 0 :(得分:1)
您需要注册为通知观察员,以查看键盘何时出现并消失。然后,您可以在显示时移动视图,或者在隐藏时将其恢复为原始视图。
我的实现通过键盘高度移动整个视图,但如果你想要,你可以只移动UITextField。
您可以查看本教程,或在答案中查看我的实现:
http://www.ioscreator.com/tutorials/move-view-behind-keyboard-ios8-swift
在viewDidAppear
override func viewWillAppear(animated:Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
然后,您需要使用方法在keyboardWillShow
和keyboardWillHide
新方法中移动视图。
func keyboardWillShow(notification: NSNotification) {
var info:NSDictionary = notification.userInfo!
var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as NSValue).CGRectValue()
var keyboardHeight:CGFloat = keyboardSize.height
var animationDuration:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as CGFloat
UIView.animateWithDuration(0.25, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
self.view.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight)
}, completion: nil)
}
并将视图移至原始状态:
func keyboardWillHide(notification: NSNotification) {
var info:NSDictionary = notification.userInfo!
var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as NSValue).CGRectValue()
var keyboardHeight:CGFloat = keyboardSize.height
var animationDuration:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as CGFloat
UIView.animateWithDuration(0.25, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
self.view.transform = CGAffineTransformIdentity
}, completion: nil)
}
最后删除viewDidDisappear
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}
答案 1 :(得分:1)
与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];
}
答案 2 :(得分:1)
以下是我为该问题所做的工作。我希望这种方法可以解决你的问题。
这是您要使用的文字字段。
@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;
}
我希望这对你有用。
答案 3 :(得分:0)
虽然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
可用于查看最新的用户输入。您可以使用当前值存储局部变量,并将其与“新”值进行比较,以查看已更改的内容。